psp: Add support for dev-assoc/disassoc
Subsystems (5)
v11 cover psp: Add support for dev-assoc/disassoc
From: Wei Wang <[email protected]>
The main purpose of this feature is to associate virtual devices like veth or netkit with a real PSP device, so we could provide PSP functionality to the application running with virtual devices.
A typical deployment that works with this feature is as follows:
Host Namespace:
psp_dev_local ←──physically linked──→ psp_dev_peer
(PSP device)
│
│ BPF on psp_dev_local ingress: bpf_redirect_peer() to nk_guest │
nk_host / veth_host
│
│ BPF on nk_host ingress: bpf_redirect_neigh() to psp_dev_local │
Guest Namespace (netns):
│
nk_guest / veth_guest
★ PSP application run here
Remote Namespace (_netns):
psp_dev_peer
★ PSP server application runs here
Note:
The general requirement for this feature to work:
For PSP to work correctly, the egress device at validate_xmit_skb() time must have psp_dev matching the association's psd. Any device stacking or traffic redirection that changes the egress device will cause either:
1. TX validation failure (SKB_DROP_REASON_PSP_OUTPUT) - fail-safe
2. RX policy failure after tx-assoc - packets without PSP extension are rejected by receiver expecting encrypted traffic
Here are a few examples that this feature would not work:
- Bonding with load balancing in round-robin, XOR, 802.3ad mode across multiple PSP devices, or mixed PSP and non-PSP devices
- Bonding with active-backup mode might work without PSP migration for failover case.
- ipvlan/macvlan in bridge mode would not work given packets are loopbacked locally without going through the PSP device.
Changes since v10:
- Corrected typo on patch 1
- Removed the kdoc style comments, Use goto style in psp_nl_dev_assoc_doit() clean up code, Resolved "TOCTOU" issue in psp_assoc_device_get_locked() in patch 2
- Replaced psp_devs_lock with a new mutex in psp_attach_netdev_notifier(), Fixed kdoc style comments in patch 3
Changes since v9:
- Added comments for psp_device_get_locked(), fixed lint issue, fixed rcu warning in patch 2
- Return error if register_netdevice_notifier() fails in psp_device_get_locked_dev_assoc() in patch 3
- Removed psp version and ip version for unnecessary tests cases in patch 5
Changes since v8:
- Rebase
Changes since v7:
- Refactor in patch 1 to have a common helper for psp_device_get_locked_admin() and psp_device_get_locked()
- Take psd->lock in psp_assoc_device_get_locked() before psp_dev_check_access() in patch 2
- Use cmpxchg() for assoc_dev->psp_dev assignment when doing dev-assoc in patch 2
- Check for err for register_netdevice_notifier() in patch 3
- Call psp_attach_netdev_notifier() in pre_doit handler for dev-assoc to avoid releasing of psd->lock in patch 3
Changes since v6:
- Remove the unused remote_addr, nk_guest_addr and import cmd in patch 5
Changes since v5:
- Remove module_exit() in patch 3
Changes since v4:
- Address compilation warning in patch 3
- Removed the call to psp_nl_has_listeners_any_ns() and check listeners when looping through netns in psp_nl_notify_dev() in patch 2. This makes sure we only send notification to netns that has listeners.
Changes since v3:
- Make nsid optional for dev-assoc/dev-disassoc operation, and use the ns user is in when it's not specified. Also added a test for this.
- Fix psp_nl_notify_dev() to compute the correct nsid relative to the listener's netns.
- Only register the new netdev event for psp dev cleanup upon the first successful dev-assoc operation.
- Change the following in selftest:
- Add CONFIG_NETKIT to driver/net's config
- Fall back to NetDrvEpEnv and run basic test cases if NetDrvContEnv does not load
- Use ksft_variants instead of psp_ip_ver_test_builder
Changes since v2:
- Change the newly added parameter to psp_device_get_and_lock() to admin in patch 1. Introduce 2 device check functions:
- psp_device_get_locked_admin() for dev-set and key-rotate
- psp_device_get_locked() for all other operations
Flip the logic for checking the dev_assoc_list accordingly in patch 2.
- Move psp_nl_notify_dev() before removing the dev from assoc_dev_list in psp_nl_dev_disassoc_doit() and correct the typo in commit msg in patch 2.
- Remove the threading and subprocess and some comment updates in patch 5.
Changes since v1:
- Update the first 4 patches to reflect the latest changes in
https://lore.kernel.org/netdev/[email protected]/
- Update patch 9 to add a param to NetDrvContEnv to control the loading of the tx forwarding bpf program
Wei Wang (5):
psp: add admin/non-admin version of psp_device_get_locked
psp: add new netlink cmd for dev-assoc and dev-disassoc
psp: add a new netdev event for dev unregister selftests/net: Add bpf skb forwarding program selftest/net: psp: Add test for dev-assoc/disassoc
Documentation/netlink/specs/psp.yaml | 71 ++- include/net/psp/types.h | 15 + include/uapi/linux/psp.h | 13 + net/psp/psp-nl-gen.c | 36 +- net/psp/psp-nl-gen.h | 7 + net/psp/psp.h | 3 +- net/psp/psp_main.c | 102 +++- net/psp/psp_nl.c | 374 +++++++++++++- tools/testing/selftests/drivers/net/config | 1 + .../drivers/net/hw/nk_redirect.bpf.c | 60 +++ .../selftests/drivers/net/lib/py/env.py | 54 ++- tools/testing/selftests/drivers/net/psp.py | 457 ++++++++++++++++-- 12 files changed, 1132 insertions(+), 61 deletions(-) create mode 100644 tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c
Signature · 1 line
2.52.0
PATCH v11 1/5 psp: add admin/non-admin version of psp_device_get_locked
From: Wei Wang <[email protected]>
Introduce 2 versions of psp_device_get_locked:
1. psp_device_get_locked_admin(): This version is used for operations that would change the status of the psd, and are currently used for dev-set and key-rotation.
2. psp_device_get_locked(): This is the non-admin version, which are used for broader user issued operations including: dev-get, rx-assoc, tx-assoc, get-stats.
Following commit will be implementing both of the checks.
--- Documentation/netlink/specs/psp.yaml | 4 ++-- net/psp/psp-nl-gen.c | 4 ++-- net/psp/psp-nl-gen.h | 2 ++ net/psp/psp.h | 2 +- net/psp/psp_main.c | 7 +++++- net/psp/psp_nl.c | 33 ++++++++++++++++++++-------- 6 files changed, 37 insertions(+), 15 deletions(-)
Documentation/netlink/specs/psp.yaml +2 -2
@@ -195,7 +195,7 @@ operations: - psp-versions-ena reply: attributes: [] - pre: psp-device-get-locked + pre: psp-device-get-locked-admin post: psp-device-unlock - name: dev-change-ntf
@@ -214,7 +214,7 @@ operations: reply: attributes: - id - pre: psp-device-get-locked + pre: psp-device-get-locked-admin post: psp-device-unlock - name: key-rotate-ntf
net/psp/psp-nl-gen.c +2 -2
@@ -71,7 +71,7 @@ static const struct genl_split_ops psp_nl_ops[] = { }, { .cmd = PSP_CMD_DEV_SET, - .pre_doit = psp_device_get_locked, + .pre_doit = psp_device_get_locked_admin, .doit = psp_nl_dev_set_doit, .post_doit = psp_device_unlock, .policy = psp_dev_set_nl_policy,
@@ -80,7 +80,7 @@ static const struct genl_split_ops psp_nl_ops[] = { }, { .cmd = PSP_CMD_KEY_ROTATE, - .pre_doit = psp_device_get_locked, + .pre_doit = psp_device_get_locked_admin, .doit = psp_nl_key_rotate_doit, .post_doit = psp_device_unlock, .policy = psp_key_rotate_nl_policy,
net/psp/psp-nl-gen.h +2 -0
@@ -17,6 +17,8 @@ extern const struct nla_policy psp_keys_nl_policy[PSP_A_KEYS_SPI + 1];int psp_device_get_locked (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info); +int psp_device_get_locked_admin (const struct genl_split_ops *ops, +struct sk_buff *skb,struct genl_info *info);int psp_assoc_device_get_locked (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info);void
net/psp/psp.h +1 -1
@@ -14,7 +14,7 @@ extern struct xarray psp_devs;extern struct mutex psp_devs_lock;void psp_dev_free (struct psp_dev *psd); -int psp_dev_check_access (struct psp_dev *psd,struct net *net); +int psp_dev_check_access (struct psp_dev *psd,struct net *net,bool admin);void psp_nl_notify_dev (struct psp_dev *psd,u32 cmd);
net/psp/psp_main.c +6 -1
@@ -27,10 +27,15 @@ struct mutex psp_devs_lock;* psp_dev_check_access() - check if user in a given net ns can access PSP dev * @psd: PSP device structure user is trying to access * @net: net namespace user is in +* @admin: If true, only allow access from @psd's main device's netns, +* for admin operations like config changes and key rotation. +* If false, also allow access from network namespaces that have +* an associated device with @psd, for read-only and association +* management operations. * * Return: 0 if PSP device should be visible in @net, errno otherwise. */ -int psp_dev_check_access (struct psp_dev *psd,struct net *net) +int psp_dev_check_access (struct psp_dev *psd,struct net *net,bool admin) {if (dev_net (psd->main_netdev) == net)return 0 ;
net/psp/psp_nl.c +24 -9
@@ -41,7 +41,8 @@ static int psp_nl_reply_send(struct sk_buff *rsp, struct genl_info *info)/* Device stuff */ static struct psp_dev * -psp_device_get_and_lock (struct net *net,struct nlattr *dev_id) +psp_device_get_and_lock (struct net *net,struct nlattr *dev_id, +bool admin) {struct psp_dev *psd;int err;
@@ -56,7 +57,7 @@ psp_device_get_and_lock(struct net *net, struct nlattr *dev_id)mutex_lock (&psd->lock);mutex_unlock (&psp_devs_lock); - err =psp_dev_check_access (psd, net); + err =psp_dev_check_access (psd, net, admin);if (err) {mutex_unlock (&psd->lock);return ERR_PTR (err);
@@ -65,17 +66,31 @@ psp_device_get_and_lock(struct net *net, struct nlattr *dev_id)return psd; } -int psp_device_get_locked (const struct genl_split_ops *ops, -struct sk_buff *skb,struct genl_info *info) +static int __psp_device_get_locked (const struct genl_split_ops *ops, +struct sk_buff *skb,struct genl_info *info, +bool admin) {if (GENL_REQ_ATTR_CHECK (info,PSP_A_DEV_ID ))return -EINVAL ; info->user_ptr[0 ] =psp_device_get_and_lock (genl_info_net (info), - info->attrs[PSP_A_DEV_ID ]); + info->attrs[PSP_A_DEV_ID ], + admin);return PTR_ERR_OR_ZERO (info->user_ptr[0 ]); } +int psp_device_get_locked_admin (const struct genl_split_ops *ops, +struct sk_buff *skb,struct genl_info *info) +{ +return __psp_device_get_locked (ops, skb, info, true); +} + +int psp_device_get_locked (const struct genl_split_ops *ops, +struct sk_buff *skb,struct genl_info *info) +{ +return __psp_device_get_locked (ops, skb, info, false); +} +void psp_device_unlock (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info)
@@ -160,7 +175,7 @@ static int psp_nl_dev_get_dumpit_one(struct sk_buff *rsp, struct netlink_callback *cb, struct psp_dev *psd) { - if (psp_dev_check_access(psd, sock_net(rsp->sk))) + if (psp_dev_check_access(psd, sock_net(rsp->sk), false)) return 0; return psp_nl_dev_fill(psd, rsp, genl_info_dump(cb));
@@ -305,7 +320,7 @@ int psp_assoc_device_get_locked(const struct genl_split_ops *ops, psd =psp_dev_get_for_sock (socket->sk);if (psd) { - err =psp_dev_check_access (psd,genl_info_net (info)); + err =psp_dev_check_access (psd,genl_info_net (info), false);if (err) {psp_dev_put (psd); psd =NULL ;
@@ -330,7 +345,7 @@ int psp_assoc_device_get_locked(const struct genl_split_ops *ops,psp_dev_put (psd); }else { - psd =psp_device_get_and_lock (genl_info_net (info), id); + psd =psp_device_get_and_lock (genl_info_net (info), id, false);if (IS_ERR (psd)) { err =PTR_ERR (psd); goto err_sock_put;
@@ -573,7 +588,7 @@ static int psp_nl_stats_get_dumpit_one(struct sk_buff *rsp, struct netlink_callback *cb, struct psp_dev *psd) { - if (psp_dev_check_access(psd, sock_net(rsp->sk))) + if (psp_dev_check_access(psd, sock_net(rsp->sk), false)) return 0; return psp_nl_stats_fill(psd, rsp, genl_info_dump(cb));
Signature · 1 line
2.52.0
PATCH v11 2/5 psp: add new netlink cmd for dev-assoc and dev-disassoc
In reply to Wei WangFrom: Wei Wang <[email protected]>
The main purpose of this cmd is to be able to associate a non-psp-capable device (e.g. veth or netkit) with a psp device. One use case is if we create a pair of veth/netkit, and assign 1 end inside a netns, while leaving the other end within the default netns, with a real PSP device, e.g. netdevsim or a physical PSP-capable NIC. With this command, we could associate the veth/netkit inside the netns with PSP device, so the virtual device could act as PSP-capable device to initiate PSP connections, and performs PSP encryption/decryption on the real PSP device.
--- Documentation/netlink/specs/psp.yaml | 67 +++++- include/net/psp/types.h | 15 ++ include/uapi/linux/psp.h | 13 ++ net/psp/psp-nl-gen.c | 32 +++ net/psp/psp-nl-gen.h | 2 + net/psp/psp_main.c | 20 ++ net/psp/psp_nl.c | 325 ++++++++++++++++++++++++++- 7 files changed, 462 insertions(+), 12 deletions(-)
Documentation/netlink/specs/psp.yaml +66 -1
@@ -13,6 +13,17 @@ definitions: hdr0-aes-gmac-128, hdr0-aes-gmac-256] attribute-sets: + - + name: assoc-dev-info + attributes: + - + name: ifindex + doc: ifindex of an associated network device. + type: u32 + - + name: nsid + doc: Network namespace ID of the associated device. + type: s32 - name: dev attributes:
@@ -24,7 +35,9 @@ attribute-sets: min: 1 - name: ifindex - doc: ifindex of the main netdevice linked to the PSP device. + doc: | + ifindex of the main netdevice linked to the PSP device, + or the ifindex to associate with the PSP device. type: u32 - name: psp-versions-cap
@@ -38,6 +51,28 @@ attribute-sets: type: u32 enum: version enum-as-flags: true + - + name: assoc-list + doc: List of associated virtual devices. + type: nest + nested-attributes: assoc-dev-info + multi-attr: true + - + name: nsid + doc: | + Network namespace ID for the device to associate/disassociate. + Optional for dev-assoc and dev-disassoc; if not present, the + device is looked up in the caller's network namespace. + type: s32 + - + name: by-association + doc: | + Flag indicating the PSP device is an associated device from a + different network namespace. + Present when in associated namespace, absent when in primary/host + namespace. + type: flag + - name: assoc attributes:
@@ -170,6 +205,8 @@ operations: - ifindex - psp-versions-cap - psp-versions-ena + - assoc-list + - by-association pre: psp-device-get-locked post: psp-device-unlock dump:
@@ -279,6 +316,34 @@ operations: post: psp-device-unlock dump: reply: *stats-all + - + name: dev-assoc + doc: Associate a network device with a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock + - + name: dev-disassoc + doc: Disassociate a network device from a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock mcast-groups: list:
include/net/psp/types.h +15 -0
@@ -5,6 +5,7 @@#include <linux/mutex.h> #include <linux/refcount.h> +#include <net/net_trackers.h> struct netlink_ext_ack ;
@@ -43,9 +44,22 @@ struct psp_dev_config {u32 versions; }; +/** +* struct psp_assoc_dev - wrapper for associated net_device +* @dev_list: list node for psp_dev::assoc_dev_list +* @assoc_dev: the associated net_device +* @dev_tracker: tracker for the net_device reference +*/ +struct psp_assoc_dev { +struct list_head dev_list; +struct net_device *assoc_dev; +netdevice_tracker dev_tracker; +}; +/** * struct psp_dev - PSP device struct * @main_netdev: original netdevice of this PSP device +* @assoc_dev_list: list of psp_assoc_dev entries associated with this PSP device * @ops: driver callbacks * @caps: device capabilities * @drv_priv: driver priv pointer
@@ -67,6 +81,7 @@ struct psp_dev_config {*/ struct psp_dev {struct net_device *main_netdev; +struct list_head assoc_dev_list;struct psp_dev_ops *ops;struct psp_dev_caps *caps;
include/uapi/linux/psp.h +13 -0
@@ -17,11 +17,22 @@ enum psp_version { PSP_VERSION_HDR0_AES_GMAC_256, }; +enum { +PSP_A_ASSOC_DEV_INFO_IFINDEX =1 , +PSP_A_ASSOC_DEV_INFO_NSID , + + __PSP_A_ASSOC_DEV_INFO_MAX, +PSP_A_ASSOC_DEV_INFO_MAX = (__PSP_A_ASSOC_DEV_INFO_MAX -1 ) +}; +enum {PSP_A_DEV_ID =1 ,PSP_A_DEV_IFINDEX ,PSP_A_DEV_PSP_VERSIONS_CAP ,PSP_A_DEV_PSP_VERSIONS_ENA , +PSP_A_DEV_ASSOC_LIST , +PSP_A_DEV_NSID , +PSP_A_DEV_BY_ASSOCIATION , __PSP_A_DEV_MAX,PSP_A_DEV_MAX = (__PSP_A_DEV_MAX -1 )
@@ -74,6 +85,8 @@ enum {PSP_CMD_RX_ASSOC ,PSP_CMD_TX_ASSOC ,PSP_CMD_GET_STATS , +PSP_CMD_DEV_ASSOC , +PSP_CMD_DEV_DISASSOC , __PSP_CMD_MAX,PSP_CMD_MAX = (__PSP_CMD_MAX -1 )
net/psp/psp-nl-gen.c +32 -0
@@ -53,6 +53,20 @@ static const struct nla_policy psp_get_stats_nl_policy[PSP_A_STATS_DEV_ID + 1] = [PSP_A_STATS_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1), }; +/* PSP_CMD_DEV_ASSOC - do */ +static const struct nla_policy psp_dev_assoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; + +/* PSP_CMD_DEV_DISASSOC - do */ +static const struct nla_policy psp_dev_disassoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; +/* Ops table for psp */ static const struct genl_split_ops psp_nl_ops[] = { {
@@ -119,6 +133,24 @@ static const struct genl_split_ops psp_nl_ops[] = { .dumpit = psp_nl_get_stats_dumpit, .flags = GENL_CMD_CAP_DUMP, }, + { + .cmd = PSP_CMD_DEV_ASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_assoc_doit, + .post_doit = psp_device_unlock, + .policy = psp_dev_assoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO, + }, + { + .cmd = PSP_CMD_DEV_DISASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_disassoc_doit, + .post_doit = psp_device_unlock, + .policy = psp_dev_disassoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO, + }, }; static const struct genl_multicast_group psp_nl_mcgrps[] = {
net/psp/psp-nl-gen.h +2 -0
@@ -33,6 +33,8 @@ int psp_nl_rx_assoc_doit(struct sk_buff *skb, struct genl_info *info);int psp_nl_tx_assoc_doit (struct sk_buff *skb,struct genl_info *info);int psp_nl_get_stats_doit (struct sk_buff *skb,struct genl_info *info);int psp_nl_get_stats_dumpit (struct sk_buff *skb,struct netlink_callback *cb); +int psp_nl_dev_assoc_doit (struct sk_buff *skb,struct genl_info *info); +int psp_nl_dev_disassoc_doit (struct sk_buff *skb,struct genl_info *info);enum {PSP_NLGRP_MGMT ,
net/psp/psp_main.c +20 -0
@@ -37,8 +37,18 @@ struct mutex psp_devs_lock;*/ int psp_dev_check_access (struct psp_dev *psd,struct net *net,bool admin) { +struct psp_assoc_dev *entry; +if (dev_net (psd->main_netdev) == net)return 0 ; + +if (!admin) { +list_for_each_entry (entry, &psd->assoc_dev_list, dev_list) { +if (dev_net (entry->assoc_dev) == net) +return 0 ; + } + } +return -ENOENT ; }
@@ -74,6 +84,7 @@ psp_dev_create(struct net_device *netdev,return ERR_PTR (-ENOMEM ); psd->main_netdev = netdev; +INIT_LIST_HEAD (&psd->assoc_dev_list); psd->ops = psd_ops; psd->caps = psd_caps; psd->drv_priv = priv_ptr;
@@ -121,6 +132,7 @@ void psp_dev_free(struct psp_dev *psd)*/ void psp_dev_unregister (struct psp_dev *psd) { +struct psp_assoc_dev *entry, *entry_tmp;struct psp_assoc *pas, *next;mutex_lock (&psp_devs_lock);
@@ -140,6 +152,14 @@ void psp_dev_unregister(struct psp_dev *psd)list_for_each_entry_safe (pas, next, &psd->stale_assocs, assocs_list)psp_dev_tx_key_del (psd, pas); +list_for_each_entry_safe (entry, entry_tmp, &psd->assoc_dev_list, + dev_list) { +list_del (&entry->dev_list); +rcu_assign_pointer (entry->assoc_dev->psp_dev,NULL ); +netdev_put (entry->assoc_dev, &entry->dev_tracker); +kfree (entry); + } +rcu_assign_pointer (psd->main_netdev->psp_dev,NULL ); psd->ops =NULL ;
net/psp/psp_nl.c +314 -11
411 lines not on this page · raw message
Signature · 1 line
2.52.0
Paolo Abeni
maintainerRe: [PATCH v11 net-next 2/5] psp: add new netlink cmd for dev-assoc and dev-disassoc
Quoted 245 lines · Wei Wang
From: Wei Wang <[email protected]>
The main purpose of this cmd is to be able to associate a non-psp-capable device (e.g. veth or netkit) with a psp device. One use case is if we create a pair of veth/netkit, and assign 1 end inside a netns, while leaving the other end within the default netns, with a real PSP device, e.g. netdevsim or a physical PSP-capable NIC. With this command, we could associate the veth/netkit inside the netns with PSP device, so the virtual device could act as PSP-capable device to initiate PSP connections, and performs PSP encryption/decryption on the real PSP device.
Signed-off-by Wei Wang <[email protected]>Reviewed-by Daniel Zahka <[email protected]>--- Documentation/netlink/specs/psp.yaml | 67 +++++- include/net/psp/types.h | 15 ++ include/uapi/linux/psp.h | 13 ++ net/psp/psp-nl-gen.c | 32 +++ net/psp/psp-nl-gen.h | 2 + net/psp/psp_main.c | 20 ++ net/psp/psp_nl.c | 325 ++++++++++++++++++++++++++- 7 files changed, 462 insertions(+), 12 deletions(-)Documentation/netlink/specs/psp.yaml +66
-1@@ -13,6 +13,17 @@ definitions: hdr0-aes-gmac-128, hdr0-aes-gmac-256] attribute-sets: + - + name: assoc-dev-info + attributes: + - + name: ifindex + doc: ifindex of an associated network device. + type: u32 + - + name: nsid + doc: Network namespace ID of the associated device. + type: s32 - name: dev attributes:@@ -24,7 +35,9 @@ attribute-sets: min: 1 - name: ifindex - doc: ifindex of the main netdevice linked to the PSP device. + doc: | + ifindex of the main netdevice linked to the PSP device, + or the ifindex to associate with the PSP device. type: u32 - name: psp-versions-cap@@ -38,6 +51,28 @@ attribute-sets: type: u32 enum: version enum-as-flags: true + - + name: assoc-list + doc: List of associated virtual devices. + type: nest + nested-attributes: assoc-dev-info + multi-attr: true + - + name: nsid + doc: | + Network namespace ID for the device to associate/disassociate. + Optional for dev-assoc and dev-disassoc; if not present, the + device is looked up in the caller's network namespace. + type: s32 + - + name: by-association + doc: | + Flag indicating the PSP device is an associated device from a + different network namespace. + Present when in associated namespace, absent when in primary/host + namespace. + type: flag + - name: assoc attributes:@@ -170,6 +205,8 @@ operations: - ifindex - psp-versions-cap - psp-versions-ena + - assoc-list + - by-association pre: psp-device-get-locked post: psp-device-unlock dump:@@ -279,6 +316,34 @@ operations: post: psp-device-unlock dump: reply: *stats-all + - + name: dev-assoc + doc: Associate a network device with a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock + - + name: dev-disassoc + doc: Disassociate a network device from a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock mcast-groups: list:include/net/psp/types.h +15
-0@@ -5,6 +5,7 @@#include <linux/mutex.h> #include <linux/refcount.h> +#include <net/net_trackers.h> struct netlink_ext_ack ;@@ -43,9 +44,22 @@ struct psp_dev_config {u32 versions; }; +/** +* struct psp_assoc_dev - wrapper for associated net_device +* @dev_list: list node for psp_dev::assoc_dev_list +* @assoc_dev: the associated net_device +* @dev_tracker: tracker for the net_device reference +*/ +struct psp_assoc_dev { +struct list_head dev_list; +struct net_device *assoc_dev; +netdevice_tracker dev_tracker; +}; +/** * struct psp_dev - PSP device struct * @main_netdev: original netdevice of this PSP device +* @assoc_dev_list: list of psp_assoc_dev entries associated with this PSP device * @ops: driver callbacks * @caps: device capabilities * @drv_priv: driver priv pointer @@ -67,6 +81,7 @@ struct psp_dev_config {*/ struct psp_dev {struct net_device *main_netdev; +struct list_head assoc_dev_list;struct psp_dev_ops *ops;struct psp_dev_caps *caps;include/uapi/linux/psp.h +13
-0@@ -17,11 +17,22 @@ enum psp_version { PSP_VERSION_HDR0_AES_GMAC_256, }; +enum { +PSP_A_ASSOC_DEV_INFO_IFINDEX =1 , +PSP_A_ASSOC_DEV_INFO_NSID , + + __PSP_A_ASSOC_DEV_INFO_MAX, +PSP_A_ASSOC_DEV_INFO_MAX = (__PSP_A_ASSOC_DEV_INFO_MAX -1 ) +}; +enum {PSP_A_DEV_ID =1 ,PSP_A_DEV_IFINDEX ,PSP_A_DEV_PSP_VERSIONS_CAP ,PSP_A_DEV_PSP_VERSIONS_ENA , +PSP_A_DEV_ASSOC_LIST , +PSP_A_DEV_NSID , +PSP_A_DEV_BY_ASSOCIATION , __PSP_A_DEV_MAX,PSP_A_DEV_MAX = (__PSP_A_DEV_MAX -1 )@@ -74,6 +85,8 @@ enum {PSP_CMD_RX_ASSOC ,PSP_CMD_TX_ASSOC ,PSP_CMD_GET_STATS , +PSP_CMD_DEV_ASSOC , +PSP_CMD_DEV_DISASSOC , __PSP_CMD_MAX,PSP_CMD_MAX = (__PSP_CMD_MAX -1 )net/psp/psp-nl-gen.c +21
-0@@ -53,6 +53,20 @@ static const struct nla_policy psp_get_stats_nl_policy[PSP_A_STATS_DEV_ID + 1] = [PSP_A_STATS_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1), }; +/* PSP_CMD_DEV_ASSOC - do */ +static const struct nla_policy psp_dev_assoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; + +/* PSP_CMD_DEV_DISASSOC - do */ +static const struct nla_policy psp_dev_disassoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; +/* Ops table for psp */ static const struct genl_split_ops psp_nl_ops[] = { {@@ -119,6 +133,24 @@ static const struct genl_split_ops psp_nl_ops[] = { .dumpit = psp_nl_get_stats_dumpit, .flags = GENL_CMD_CAP_DUMP, }, + { + .cmd = PSP_CMD_DEV_ASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_assoc_doit, + .post_doit = psp_device_unlock, + .policy = psp_dev_assoc_nl_policy, + .maxattr = PSP_A_DEV_NSID,
net/psp/psp-nl-gen.c static const struct genl_split_ops psp_nl_ops[] = { · line 152
Quoted 10 lines
net/psp/psp-nl-gen.c +10
-0+ .flags = GENL_CMD_CAP_DO, + }, + { + .cmd = PSP_CMD_DEV_DISASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_disassoc_doit, + .post_doit = psp_device_unlock, + .policy = psp_dev_disassoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO,
Sashiko notes that the above allows deleteing an associations bypassing the netns boundaries. Do you need ADMIN_PERM flag or exlicit checks in the doit cb?
Quoted 19 lines
net/psp/psp-nl-gen.c +15
-0@@ -292,6 +455,145 @@ int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info)return err; } +int psp_nl_dev_assoc_doit (struct sk_buff *skb,struct genl_info *info) +{ +struct psp_dev *psd = info->user_ptr[0 ]; +struct psp_assoc_dev *psp_assoc_dev; +struct net_device *assoc_dev; +struct sk_buff *rsp; +u32 assoc_ifindex; +struct net *net; +int nsid, err; + +if (GENL_REQ_ATTR_CHECK (info,PSP_A_DEV_IFINDEX )) +return -EINVAL ; + +if (info->attrs[PSP_A_DEV_NSID ]) { + nsid =nla_get_s32 (info->attrs[PSP_A_DEV_NSID ]);
net/psp/psp-nl-gen.c int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) · line 482
Quoted 10 lines
net/psp/psp-nl-gen.c +10
-0+ + net =get_net_ns_by_id (genl_info_net (info), nsid); +if (!net) { +NL_SET_BAD_ATTR (info->extack, + info->attrs[PSP_A_DEV_NSID ]); +return -EINVAL ; + } + }else { + net =get_net (genl_info_net (info)); + }
psp_nl_dev_disassoc_doit() has the same code; perhaps it would be worthy move it in a common helper, called via pre_doit()? It should also simplify the cleanup paths.
Quoted 23 lines
net/psp/psp-nl-gen.c +23
-0+ + psp_assoc_dev =kzalloc (sizeof (*psp_assoc_dev),GFP_KERNEL ); +if (!psp_assoc_dev) { + err = -ENOMEM ; + goto alloc_err; + } + + assoc_ifindex =nla_get_u32 (info->attrs[PSP_A_DEV_IFINDEX ]); + assoc_dev =netdev_get_by_index (net, assoc_ifindex, + &psp_assoc_dev->dev_tracker, +GFP_KERNEL ); +if (!assoc_dev) { +NL_SET_BAD_ATTR (info->extack, info->attrs[PSP_A_DEV_IFINDEX ]); + err = -ENODEV ; + goto assoc_dev_err; + } + +/* Check if device is already associated with a PSP device */ +if (cmpxchg (&assoc_dev->psp_dev,NULL ,RCU_INITIALIZER (psd))) { +NL_SET_ERR_MSG (info->extack, +"Device already associated with a PSP device" ); + err = -EBUSY ; + goto cmpxchg_err;
net/psp/psp-nl-gen.c int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) · line 515
Quoted 10 lines
net/psp/psp-nl-gen.c +10
-0+ } + + psp_assoc_dev->assoc_dev = assoc_dev; + rsp =psp_nl_reply_new (info); +if (!rsp) { + err = -ENOMEM ; + goto rsp_err; + } + +list_add_tail (&psp_assoc_dev->dev_list, &psd->assoc_dev_list);
Sashiko says:
---list_add_tail(&psp_assoc_dev->dev_list, &psd->assoc_dev_list); There doesn't seem to be a limit on the number of devices that can be associated with a single PSP device.
If a user repeatedly associates devices, could the generated netlink message in psp_nl_dev_fill() exceed the maximum allowed size (GENLMSG_DEFAULT_SIZE), causing it to fail with -EMSGSIZE and permanently break PSP_CMD_DEV_GET and management notifications for the device?
--
/P
Re: [PATCH v11 net-next 2/5] psp: add new netlink cmd for dev-assoc and dev-disassoc
Quoted 255 lines · Paolo Abeni
Quoted 251 lines · Wei Wang
From: Wei Wang <[email protected]>
The main purpose of this cmd is to be able to associate a non-psp-capable device (e.g. veth or netkit) with a psp device. One use case is if we create a pair of veth/netkit, and assign 1 end inside a netns, while leaving the other end within the default netns, with a real PSP device, e.g. netdevsim or a physical PSP-capable NIC. With this command, we could associate the veth/netkit inside the netns with PSP device, so the virtual device could act as PSP-capable device to initiate PSP connections, and performs PSP encryption/decryption on the real PSP device.
Signed-off-by Wei Wang <[email protected]>Reviewed-by Daniel Zahka <[email protected]>--- Documentation/netlink/specs/psp.yaml | 67 +++++- include/net/psp/types.h | 15 ++ include/uapi/linux/psp.h | 13 ++ net/psp/psp-nl-gen.c | 32 +++ net/psp/psp-nl-gen.h | 2 + net/psp/psp_main.c | 20 ++ net/psp/psp_nl.c | 325 ++++++++++++++++++++++++++- 7 files changed, 462 insertions(+), 12 deletions(-)Documentation/netlink/specs/psp.yaml +66
-1@@ -13,6 +13,17 @@ definitions: hdr0-aes-gmac-128, hdr0-aes-gmac-256] attribute-sets: + - + name: assoc-dev-info + attributes: + - + name: ifindex + doc: ifindex of an associated network device. + type: u32 + - + name: nsid + doc: Network namespace ID of the associated device. + type: s32 - name: dev attributes:@@ -24,7 +35,9 @@ attribute-sets: min: 1 - name: ifindex - doc: ifindex of the main netdevice linked to the PSP device. + doc: | + ifindex of the main netdevice linked to the PSP device, + or the ifindex to associate with the PSP device. type: u32 - name: psp-versions-cap@@ -38,6 +51,28 @@ attribute-sets: type: u32 enum: version enum-as-flags: true + - + name: assoc-list + doc: List of associated virtual devices. + type: nest + nested-attributes: assoc-dev-info + multi-attr: true + - + name: nsid + doc: | + Network namespace ID for the device to associate/disassociate. + Optional for dev-assoc and dev-disassoc; if not present, the + device is looked up in the caller's network namespace. + type: s32 + - + name: by-association + doc: | + Flag indicating the PSP device is an associated device from a + different network namespace. + Present when in associated namespace, absent when in primary/host + namespace. + type: flag + - name: assoc attributes:@@ -170,6 +205,8 @@ operations: - ifindex - psp-versions-cap - psp-versions-ena + - assoc-list + - by-association pre: psp-device-get-locked post: psp-device-unlock dump:@@ -279,6 +316,34 @@ operations: post: psp-device-unlock dump: reply: *stats-all + - + name: dev-assoc + doc: Associate a network device with a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock + - + name: dev-disassoc + doc: Disassociate a network device from a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock mcast-groups: list:include/net/psp/types.h +15
-0@@ -5,6 +5,7 @@#include <linux/mutex.h> #include <linux/refcount.h> +#include <net/net_trackers.h> struct netlink_ext_ack ;@@ -43,9 +44,22 @@ struct psp_dev_config {u32 versions; }; +/** +* struct psp_assoc_dev - wrapper for associated net_device +* @dev_list: list node for psp_dev::assoc_dev_list +* @assoc_dev: the associated net_device +* @dev_tracker: tracker for the net_device reference +*/ +struct psp_assoc_dev { +struct list_head dev_list; +struct net_device *assoc_dev; +netdevice_tracker dev_tracker; +}; +/** * struct psp_dev - PSP device struct * @main_netdev: original netdevice of this PSP device +* @assoc_dev_list: list of psp_assoc_dev entries associated with this PSP device * @ops: driver callbacks * @caps: device capabilities * @drv_priv: driver priv pointer @@ -67,6 +81,7 @@ struct psp_dev_config {*/ struct psp_dev {struct net_device *main_netdev; +struct list_head assoc_dev_list;struct psp_dev_ops *ops;struct psp_dev_caps *caps;include/uapi/linux/psp.h +13
-0@@ -17,11 +17,22 @@ enum psp_version { PSP_VERSION_HDR0_AES_GMAC_256, }; +enum { +PSP_A_ASSOC_DEV_INFO_IFINDEX =1 , +PSP_A_ASSOC_DEV_INFO_NSID , + + __PSP_A_ASSOC_DEV_INFO_MAX, +PSP_A_ASSOC_DEV_INFO_MAX = (__PSP_A_ASSOC_DEV_INFO_MAX -1 ) +}; +enum {PSP_A_DEV_ID =1 ,PSP_A_DEV_IFINDEX ,PSP_A_DEV_PSP_VERSIONS_CAP ,PSP_A_DEV_PSP_VERSIONS_ENA , +PSP_A_DEV_ASSOC_LIST , +PSP_A_DEV_NSID , +PSP_A_DEV_BY_ASSOCIATION , __PSP_A_DEV_MAX,PSP_A_DEV_MAX = (__PSP_A_DEV_MAX -1 )@@ -74,6 +85,8 @@ enum {PSP_CMD_RX_ASSOC ,PSP_CMD_TX_ASSOC ,PSP_CMD_GET_STATS , +PSP_CMD_DEV_ASSOC , +PSP_CMD_DEV_DISASSOC , __PSP_CMD_MAX,PSP_CMD_MAX = (__PSP_CMD_MAX -1 )net/psp/psp-nl-gen.c +27
-0@@ -53,6 +53,20 @@ static const struct nla_policy psp_get_stats_nl_policy[PSP_A_STATS_DEV_ID + 1] = [PSP_A_STATS_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1), }; +/* PSP_CMD_DEV_ASSOC - do */ +static const struct nla_policy psp_dev_assoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; + +/* PSP_CMD_DEV_DISASSOC - do */ +static const struct nla_policy psp_dev_disassoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; +/* Ops table for psp */ static const struct genl_split_ops psp_nl_ops[] = { {@@ -119,6 +133,24 @@ static const struct genl_split_ops psp_nl_ops[] = { .dumpit = psp_nl_get_stats_dumpit, .flags = GENL_CMD_CAP_DUMP, }, + { + .cmd = PSP_CMD_DEV_ASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_assoc_doit, + .post_doit = psp_device_unlock, + .policy = psp_dev_assoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO, + }, + { + .cmd = PSP_CMD_DEV_DISASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_disassoc_doit,
Quoted 8 lines
net/psp/psp-nl-gen.c static const struct genl_split_ops psp_nl_ops[] = { · line 152
Quoted 4 lines
net/psp/psp-nl-gen.c +4
-0+ .post_doit = psp_device_unlock, + .policy = psp_dev_disassoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO,Sashiko notes that the above allows deleteing an associations bypassing the netns boundaries. Do you need
ADMIN_PERMflag or exlicit checks in the doit cb?
I think the concern is if we are calling this from an assoc_dev's netns, it should not allow it to delete any assoc_dev from other assoc_dev's netns. Right?
I will add a check to only allow deletion of assoc_dev from its own netns. (except main_dev's netns).
Quoted 26 lines
Quoted 26 lines
net/psp/psp-nl-gen.c +21
-0@@ -292,6 +455,145 @@ int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info)return err; } +int psp_nl_dev_assoc_doit (struct sk_buff *skb,struct genl_info *info) +{ +struct psp_dev *psd = info->user_ptr[0 ]; +struct psp_assoc_dev *psp_assoc_dev; +struct net_device *assoc_dev; +struct sk_buff *rsp; +u32 assoc_ifindex; +struct net *net; +int nsid, err; + +if (GENL_REQ_ATTR_CHECK (info,PSP_A_DEV_IFINDEX )) +return -EINVAL ; + +if (info->attrs[PSP_A_DEV_NSID ]) { + nsid =nla_get_s32 (info->attrs[PSP_A_DEV_NSID ]); + + net =get_net_ns_by_id (genl_info_net (info), nsid); +if (!net) { +NL_SET_BAD_ATTR (info->extack, + info->attrs[PSP_A_DEV_NSID ]); +return -EINVAL ;
Quoted 9 lines
net/psp/psp-nl-gen.c int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) · line 482
Quoted 4 lines
net/psp/psp-nl-gen.c +4
-0+ } + }else { + net =get_net (genl_info_net (info)); + }
psp_nl_dev_disassoc_doit()has the same code; perhaps it would be worthy move it in a common helper, called viapre_doit()? It should also simplify the cleanup paths.
Ack.
Quoted 36 lines
Quoted 23 lines
net/psp/psp-nl-gen.c +23
-0+ + psp_assoc_dev =kzalloc (sizeof (*psp_assoc_dev),GFP_KERNEL ); +if (!psp_assoc_dev) { + err = -ENOMEM ; + goto alloc_err; + } + + assoc_ifindex =nla_get_u32 (info->attrs[PSP_A_DEV_IFINDEX ]); + assoc_dev =netdev_get_by_index (net, assoc_ifindex, + &psp_assoc_dev->dev_tracker, +GFP_KERNEL ); +if (!assoc_dev) { +NL_SET_BAD_ATTR (info->extack, info->attrs[PSP_A_DEV_IFINDEX ]); + err = -ENODEV ; + goto assoc_dev_err; + } + +/* Check if device is already associated with a PSP device */ +if (cmpxchg (&assoc_dev->psp_dev,NULL ,RCU_INITIALIZER (psd))) { +NL_SET_ERR_MSG (info->extack, +"Device already associated with a PSP device" ); + err = -EBUSY ; + goto cmpxchg_err;net/psp/psp-nl-gen.c int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) · line 515
Quoted 10 lines
net/psp/psp-nl-gen.c +10
-0+ } + + psp_assoc_dev->assoc_dev = assoc_dev; + rsp =psp_nl_reply_new (info); +if (!rsp) { + err = -ENOMEM ; + goto rsp_err; + } + +list_add_tail (&psp_assoc_dev->dev_list, &psd->assoc_dev_list);Sashiko says:
Quoted 9 lines
---
list_add_tail(&psp_assoc_dev->dev_list, &psd->assoc_dev_list); There doesn't seem to be a limit on the number of devices that can be associated with a single PSP device.
If a user repeatedly associates devices, could the generated netlink message inpsp_nl_dev_fill()exceed the maximum allowed size (GENLMSG_DEFAULT_SIZE), causing it to fail with-EMSGSIZEand permanently breakPSP_CMD_DEV_GETand management notifications for the device?
--
Ack. Will enforce a max allowed number on the assoc_dev_list to fit into GENLMSG_DEFAULT_SIZE.
Quoted 3 lines
/P
Re: [PATCH v11 net-next 2/5] psp: add new netlink cmd for dev-assoc and dev-disassoc
Quoted 266 lines · Wei Wang
Quoted 255 lines · Paolo Abeni
Quoted 251 lines · Wei Wang
From: Wei Wang <[email protected]>
The main purpose of this cmd is to be able to associate a non-psp-capable device (e.g. veth or netkit) with a psp device. One use case is if we create a pair of veth/netkit, and assign 1 end inside a netns, while leaving the other end within the default netns, with a real PSP device, e.g. netdevsim or a physical PSP-capable NIC. With this command, we could associate the veth/netkit inside the netns with PSP device, so the virtual device could act as PSP-capable device to initiate PSP connections, and performs PSP encryption/decryption on the real PSP device.
Signed-off-by Wei Wang <[email protected]>Reviewed-by Daniel Zahka <[email protected]>--- Documentation/netlink/specs/psp.yaml | 67 +++++- include/net/psp/types.h | 15 ++ include/uapi/linux/psp.h | 13 ++ net/psp/psp-nl-gen.c | 32 +++ net/psp/psp-nl-gen.h | 2 + net/psp/psp_main.c | 20 ++ net/psp/psp_nl.c | 325 ++++++++++++++++++++++++++- 7 files changed, 462 insertions(+), 12 deletions(-)Documentation/netlink/specs/psp.yaml +66
-1@@ -13,6 +13,17 @@ definitions: hdr0-aes-gmac-128, hdr0-aes-gmac-256] attribute-sets: + - + name: assoc-dev-info + attributes: + - + name: ifindex + doc: ifindex of an associated network device. + type: u32 + - + name: nsid + doc: Network namespace ID of the associated device. + type: s32 - name: dev attributes:@@ -24,7 +35,9 @@ attribute-sets: min: 1 - name: ifindex - doc: ifindex of the main netdevice linked to the PSP device. + doc: | + ifindex of the main netdevice linked to the PSP device, + or the ifindex to associate with the PSP device. type: u32 - name: psp-versions-cap@@ -38,6 +51,28 @@ attribute-sets: type: u32 enum: version enum-as-flags: true + - + name: assoc-list + doc: List of associated virtual devices. + type: nest + nested-attributes: assoc-dev-info + multi-attr: true + - + name: nsid + doc: | + Network namespace ID for the device to associate/disassociate. + Optional for dev-assoc and dev-disassoc; if not present, the + device is looked up in the caller's network namespace. + type: s32 + - + name: by-association + doc: | + Flag indicating the PSP device is an associated device from a + different network namespace. + Present when in associated namespace, absent when in primary/host + namespace. + type: flag + - name: assoc attributes:@@ -170,6 +205,8 @@ operations: - ifindex - psp-versions-cap - psp-versions-ena + - assoc-list + - by-association pre: psp-device-get-locked post: psp-device-unlock dump:@@ -279,6 +316,34 @@ operations: post: psp-device-unlock dump: reply: *stats-all + - + name: dev-assoc + doc: Associate a network device with a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock + - + name: dev-disassoc + doc: Disassociate a network device from a PSP device. + attribute-set: dev + do: + request: + attributes: + - id + - ifindex + - nsid + reply: + attributes: [] + pre: psp-device-get-locked + post: psp-device-unlock mcast-groups: list:include/net/psp/types.h +15
-0@@ -5,6 +5,7 @@#include <linux/mutex.h> #include <linux/refcount.h> +#include <net/net_trackers.h> struct netlink_ext_ack ;@@ -43,9 +44,22 @@ struct psp_dev_config {u32 versions; }; +/** +* struct psp_assoc_dev - wrapper for associated net_device +* @dev_list: list node for psp_dev::assoc_dev_list +* @assoc_dev: the associated net_device +* @dev_tracker: tracker for the net_device reference +*/ +struct psp_assoc_dev { +struct list_head dev_list; +struct net_device *assoc_dev; +netdevice_tracker dev_tracker; +}; +/** * struct psp_dev - PSP device struct * @main_netdev: original netdevice of this PSP device +* @assoc_dev_list: list of psp_assoc_dev entries associated with this PSP device * @ops: driver callbacks * @caps: device capabilities * @drv_priv: driver priv pointer @@ -67,6 +81,7 @@ struct psp_dev_config {*/ struct psp_dev {struct net_device *main_netdev; +struct list_head assoc_dev_list;struct psp_dev_ops *ops;struct psp_dev_caps *caps;include/uapi/linux/psp.h +13
-0@@ -17,11 +17,22 @@ enum psp_version { PSP_VERSION_HDR0_AES_GMAC_256, }; +enum { +PSP_A_ASSOC_DEV_INFO_IFINDEX =1 , +PSP_A_ASSOC_DEV_INFO_NSID , + + __PSP_A_ASSOC_DEV_INFO_MAX, +PSP_A_ASSOC_DEV_INFO_MAX = (__PSP_A_ASSOC_DEV_INFO_MAX -1 ) +}; +enum {PSP_A_DEV_ID =1 ,PSP_A_DEV_IFINDEX ,PSP_A_DEV_PSP_VERSIONS_CAP ,PSP_A_DEV_PSP_VERSIONS_ENA , +PSP_A_DEV_ASSOC_LIST , +PSP_A_DEV_NSID , +PSP_A_DEV_BY_ASSOCIATION , __PSP_A_DEV_MAX,PSP_A_DEV_MAX = (__PSP_A_DEV_MAX -1 )@@ -74,6 +85,8 @@ enum {PSP_CMD_RX_ASSOC ,PSP_CMD_TX_ASSOC ,PSP_CMD_GET_STATS , +PSP_CMD_DEV_ASSOC , +PSP_CMD_DEV_DISASSOC , __PSP_CMD_MAX,PSP_CMD_MAX = (__PSP_CMD_MAX -1 )net/psp/psp-nl-gen.c +27
-0@@ -53,6 +53,20 @@ static const struct nla_policy psp_get_stats_nl_policy[PSP_A_STATS_DEV_ID + 1] = [PSP_A_STATS_DEV_ID] = NLA_POLICY_MIN(NLA_U32, 1), }; +/* PSP_CMD_DEV_ASSOC - do */ +static const struct nla_policy psp_dev_assoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; + +/* PSP_CMD_DEV_DISASSOC - do */ +static const struct nla_policy psp_dev_disassoc_nl_policy[PSP_A_DEV_NSID +1 ] = { + [PSP_A_DEV_ID ] =NLA_POLICY_MIN (NLA_U32 ,1 ), + [PSP_A_DEV_IFINDEX ] = { .type =NLA_U32 , }, + [PSP_A_DEV_NSID ] = { .type =NLA_S32 , }, +}; +/* Ops table for psp */ static const struct genl_split_ops psp_nl_ops[] = { {@@ -119,6 +133,24 @@ static const struct genl_split_ops psp_nl_ops[] = { .dumpit = psp_nl_get_stats_dumpit, .flags = GENL_CMD_CAP_DUMP, }, + { + .cmd = PSP_CMD_DEV_ASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_assoc_doit, + .post_doit = psp_device_unlock, + .policy = psp_dev_assoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO, + }, + { + .cmd = PSP_CMD_DEV_DISASSOC, + .pre_doit = psp_device_get_locked, + .doit = psp_nl_dev_disassoc_doit,Quoted 8 lines
net/psp/psp-nl-gen.c static const struct genl_split_ops psp_nl_ops[] = { · line 152
Quoted 4 lines
net/psp/psp-nl-gen.c +4
-0+ .post_doit = psp_device_unlock, + .policy = psp_dev_disassoc_nl_policy, + .maxattr = PSP_A_DEV_NSID, + .flags = GENL_CMD_CAP_DO,Sashiko notes that the above allows deleteing an associations bypassing the netns boundaries. Do you need
ADMIN_PERMflag or exlicit checks in the doit cb?
Quoted 6 lines
I think the concern is if we are calling this from an assoc_dev's netns, it should not allow it to delete any
assoc_devfrom other assoc_dev's netns. Right?
I will add a check to only allow deletion ofassoc_devfrom its own netns. (except main_dev's netns).
Further on this point, I think a cleaner solution is to just reject user passing in nsid if the user is not in dev_net(psd->main_netdev). In such situation, we should only do dev-assoc/dev-disassoc within the current user netns. So no point of passing in nsid.
We only allow nsid to be passed in if the user is in dev_net(psd->main_netdev), i.e. admin user.
Let me know your thoughts on this. @Daniel Zahka @Paolo Abeni
Quoted 90 lines
Quoted 26 lines
Quoted 26 lines
net/psp/psp-nl-gen.c +21
-0@@ -292,6 +455,145 @@ int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info)return err; } +int psp_nl_dev_assoc_doit (struct sk_buff *skb,struct genl_info *info) +{ +struct psp_dev *psd = info->user_ptr[0 ]; +struct psp_assoc_dev *psp_assoc_dev; +struct net_device *assoc_dev; +struct sk_buff *rsp; +u32 assoc_ifindex; +struct net *net; +int nsid, err; + +if (GENL_REQ_ATTR_CHECK (info,PSP_A_DEV_IFINDEX )) +return -EINVAL ; + +if (info->attrs[PSP_A_DEV_NSID ]) { + nsid =nla_get_s32 (info->attrs[PSP_A_DEV_NSID ]); + + net =get_net_ns_by_id (genl_info_net (info), nsid); +if (!net) { +NL_SET_BAD_ATTR (info->extack, + info->attrs[PSP_A_DEV_NSID ]); +return -EINVAL ;Quoted 9 lines
net/psp/psp-nl-gen.c int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) · line 482
Quoted 4 lines
net/psp/psp-nl-gen.c +4
-0+ } + }else { + net =get_net (genl_info_net (info)); + }
psp_nl_dev_disassoc_doit()has the same code; perhaps it would be worthy move it in a common helper, called viapre_doit()? It should also simplify the cleanup paths.Ack.
Quoted 36 lines
Quoted 23 lines
net/psp/psp-nl-gen.c +23
-0+ + psp_assoc_dev =kzalloc (sizeof (*psp_assoc_dev),GFP_KERNEL ); +if (!psp_assoc_dev) { + err = -ENOMEM ; + goto alloc_err; + } + + assoc_ifindex =nla_get_u32 (info->attrs[PSP_A_DEV_IFINDEX ]); + assoc_dev =netdev_get_by_index (net, assoc_ifindex, + &psp_assoc_dev->dev_tracker, +GFP_KERNEL ); +if (!assoc_dev) { +NL_SET_BAD_ATTR (info->extack, info->attrs[PSP_A_DEV_IFINDEX ]); + err = -ENODEV ; + goto assoc_dev_err; + } + +/* Check if device is already associated with a PSP device */ +if (cmpxchg (&assoc_dev->psp_dev,NULL ,RCU_INITIALIZER (psd))) { +NL_SET_ERR_MSG (info->extack, +"Device already associated with a PSP device" ); + err = -EBUSY ; + goto cmpxchg_err;net/psp/psp-nl-gen.c int psp_nl_key_rotate_doit(struct sk_buff *skb, struct genl_info *info) · line 515
Quoted 10 lines
net/psp/psp-nl-gen.c +10
-0+ } + + psp_assoc_dev->assoc_dev = assoc_dev; + rsp =psp_nl_reply_new (info); +if (!rsp) { + err = -ENOMEM ; + goto rsp_err; + } + +list_add_tail (&psp_assoc_dev->dev_list, &psd->assoc_dev_list);Sashiko says:
Quoted 9 lines
---
list_add_tail(&psp_assoc_dev->dev_list, &psd->assoc_dev_list); There doesn't seem to be a limit on the number of devices that can be associated with a single PSP device.
If a user repeatedly associates devices, could the generated netlink message inpsp_nl_dev_fill()exceed the maximum allowed size (GENLMSG_DEFAULT_SIZE), causing it to fail with-EMSGSIZEand permanently breakPSP_CMD_DEV_GETand management notifications for the device?
--Ack. Will enforce a max allowed number on the
assoc_dev_listto fit intoGENLMSG_DEFAULT_SIZE.Quoted 3 lines
/P
PATCH v11 3/5 psp: add a new netdev event for dev unregister
In reply to Wei WangFrom: Wei Wang <[email protected]>
Add a new netdev event for dev unregister and handle the removal of this dev from psp->assoc_dev_list, upon the first dev-assoc operation.
--- Documentation/netlink/specs/psp.yaml | 2 +- net/psp/psp-nl-gen.c | 2 +- net/psp/psp-nl-gen.h | 3 ++ net/psp/psp.h | 1 + net/psp/psp_main.c | 75 ++++++++++++++++++++++++++++ net/psp/psp_nl.c | 16 ++++++ 6 files changed, 97 insertions(+), 2 deletions(-)
Documentation/netlink/specs/psp.yaml +1 -1
@@ -328,7 +328,7 @@ operations: - nsid reply: attributes: [] - pre: psp-device-get-locked + pre: psp-device-get-locked-dev-assoc post: psp-device-unlock - name: dev-disassoc
net/psp/psp-nl-gen.c +1 -1
@@ -135,7 +135,7 @@ static const struct genl_split_ops psp_nl_ops[] = { }, { .cmd = PSP_CMD_DEV_ASSOC, - .pre_doit = psp_device_get_locked, + .pre_doit = psp_device_get_locked_dev_assoc, .doit = psp_nl_dev_assoc_doit, .post_doit = psp_device_unlock, .policy = psp_dev_assoc_nl_policy,
net/psp/psp-nl-gen.h +3 -0
@@ -21,6 +21,9 @@ int psp_device_get_locked_admin(const struct genl_split_ops *ops, struct sk_buff *skb, struct genl_info *info);int psp_assoc_device_get_locked (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info); +int psp_device_get_locked_dev_assoc (const struct genl_split_ops *ops, +struct sk_buff *skb, +struct genl_info *info);void psp_device_unlock (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info);
net/psp/psp.h +1 -0
@@ -15,6 +15,7 @@ extern struct mutex psp_devs_lock;void psp_dev_free (struct psp_dev *psd);int psp_dev_check_access (struct psp_dev *psd,struct net *net,bool admin); +int psp_attach_netdev_notifier (void );void psp_nl_notify_dev (struct psp_dev *psd,u32 cmd);
net/psp/psp_main.c +75 -0
81 lines not on this page · raw message
net/psp/psp_nl.c +16 -0
@@ -167,6 +167,22 @@ int psp_device_get_locked(const struct genl_split_ops *ops,return __psp_device_get_locked (ops, skb, info, false); } +/* +* Non-admin version of psp_device_get_locked() + psp_attach_netdev_notifier() +* only used for dev-assoc. +*/ +int psp_device_get_locked_dev_assoc (const struct genl_split_ops *ops, +struct sk_buff *skb,struct genl_info *info) +{ +int err; + + err =psp_attach_netdev_notifier (); +if (err) +return err; + +return __psp_device_get_locked (ops, skb, info, false); +} +void psp_device_unlock (const struct genl_split_ops *ops,struct sk_buff *skb,struct genl_info *info)
Signature · 1 line
2.52.0
Paolo Abeni
maintainerRe: [PATCH v11 net-next 3/5] psp: add a new netdev event for dev unregister
Quoted 7 lines · Wei Wang
net/psp/psp_main.c +7
-0+static int psp_netdev_event (struct notifier_block *nb,unsigned long event, +void *ptr) +{ +struct net_device *dev =netdev_notifier_info_to_dev (ptr); +struct psp_dev *psd; + +if (event !=NETDEV_UNREGISTER )
net/psp/psp_main.c int psp_dev_rcv(struct sk_buff *skb, u16 dev_id, u8 generation, bool strip_icv) · line 409
Quoted 10 lines
net/psp/psp_main.c +10
-0+return NOTIFY_DONE ; + +rcu_read_lock (); + psd =rcu_dereference (dev->psp_dev); +if (psd &&psp_dev_tryget (psd)) { +rcu_read_unlock (); +mutex_lock (&psd->lock); +psp_dev_disassoc_one (psd, dev); +mutex_unlock (&psd->lock); +psp_dev_put (psd);
Sashiko notes that the above is racy:
---
Can this code race with psp_nl_dev_assoc_doit() and permanently leak a net_device reference?
If CPU1 is executing psp_nl_dev_assoc_doit() and CPU2 is unregistering the device, the following interleaving could happen:
CPU1 (psp_nl_dev_assoc_doit)
assoc_dev = dev_get_by_index(...) // acquires a reference
CPU2 (unregister_netdevice)
psp_netdev_event()
psd = rcu_dereference(dev->psp_dev); // sees NULL, returns
NOTIFY_DONE
CPU1 (psp_nl_dev_assoc_doit)
cmpxchg(&assoc_dev->psp_dev, NULL, psd); // succeeds!
list_add(...) // adds to psd->assoc_dev_listIf this occurs, the notifier misses the unregistration event since it runs before the device is fully associated. The unregistering thread will then enter netdev_wait_allrefs() and wait indefinitely because the reference held in assoc_dev_list is never released.
---
/P
Re: [PATCH v11 net-next 3/5] psp: add a new netdev event for dev unregister
Quoted 36 lines · Paolo Abeni
Quoted 7 lines · Wei Wang
net/psp/psp_main.c +7
-0+static int psp_netdev_event (struct notifier_block *nb,unsigned long event, +void *ptr) +{ +struct net_device *dev =netdev_notifier_info_to_dev (ptr); +struct psp_dev *psd; + +if (event !=NETDEV_UNREGISTER )net/psp/psp_main.c int psp_dev_rcv(struct sk_buff *skb, u16 dev_id, u8 generation, bool strip_icv) · line 409
Quoted 10 lines
net/psp/psp_main.c +10
-0+return NOTIFY_DONE ; + +rcu_read_lock (); + psd =rcu_dereference (dev->psp_dev); +if (psd &&psp_dev_tryget (psd)) { +rcu_read_unlock (); +mutex_lock (&psd->lock); +psp_dev_disassoc_one (psd, dev); +mutex_unlock (&psd->lock); +psp_dev_put (psd);Sashiko notes that the above is racy:
---
Can this code race withpsp_nl_dev_assoc_doit()and permanently leak anet_devicereference?
If CPU1 is executingpsp_nl_dev_assoc_doit()and CPU2 is unregistering the device, the following interleaving could happen:
CPU1 (psp_nl_dev_assoc_doit)
assoc_dev=dev_get_by_index(...)// acquires a reference
CPU2 (unregister_netdevice)psp_netdev_event() psd = rcu_dereference(dev->psp_dev); // sees NULL, returns NOTIFY_DONE CPU1 (psp_nl_dev_assoc_doit) cmpxchg(&assoc_dev->psp_dev, NULL, psd); // succeeds! list_add(...) // adds to psd->assoc_dev_list
Quoted 6 lines
If this occurs, the notifier misses the unregistration event since it runs before the device is fully associated. The unregistering thread will then enter
netdev_wait_allrefs()and wait indefinitely because the reference held inassoc_dev_listis never released.
---
Hmm... Good catch. I think I could do the following to fix this:
On psp_nl_dev_assoc_doit():
- grab psd->lock
- Do cmpxchg(&assoc_dev->psp_dev, NULL, psd)
- Do if (READ_ONCE(assoc_dev->reg_state) != NETREG_REGISTERED) {
err = -ENODEV;
goto cleanup;
}On unregister_netdevice side, the sequence of calls are as follows in unregister_netdevice_many_notify():
- WRITE_ONCE(reg_state, NETREG_UNREGISTERING);
- synchronize_net()
- call_netdevice_notifiers(NETDEV_UNREGISTER, dev) => psp_netdev_event() => Read psd => grab psd->lock and do clean up if psd != NULL
With this logic, at least 1 side would do the cleanup. And the clean up path is guarded by psd->lock so it is also sequenced and should not have double free issue.
Let me know what you think.
Quoted 2 lines
/P
PATCH v11 4/5 selftests/net: Add bpf skb forwarding program
In reply to Wei WangFrom: Wei Wang <[email protected]>
Add nk_redirect.bpf.c, a BPF program that forwards skbs matching some IPv6 prefix received on eth0 ifindex to a specified dev ifindex. bpf_redirect_neigh() is used to make sure neighbor lookup is performed and proper MAC addr is being used.
--- .../drivers/net/hw/nk_redirect.bpf.c | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c
tools/testing/selftests/drivers/net/hw/nk_redirect.bpf.c new file +60 -0
@@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* BPF program for redirecting traffic using bpf_redirect_neigh(). +* Unlike bpf_redirect() which preserves L2 headers, bpf_redirect_neigh() +* performs neighbor lookup and fills in the correct L2 addresses for the +* target interface. This is necessary when redirecting across different +* device types (e.g., from netdevsim to netkit). +*/ +#include <linux/bpf.h> +#include <linux/pkt_cls.h> +#include <linux/if_ether.h> +#include <linux/ipv6.h> +#include <linux/in6.h> +#include <bpf/bpf_endian.h> +#include <bpf/bpf_helpers.h> + +#define TC_ACT_OK 0 +#define ETH_P_IPV6 0x86DD + +#define ctx_ptr (field) ((void *)(long)(field)) + +#define v6_p64_equal (a, b) (a.s6_addr32[0] == b.s6_addr32[0] && \ + a.s6_addr32[1] == b.s6_addr32[1]) + +volatile __u32 redirect_ifindex; +volatile __u8 ipv6_prefix[16 ]; + +SEC("tc/ingress") +int tc_redirect (struct __sk_buff *skb) +{ +void *data_end =ctx_ptr (skb->data_end); +void *data =ctx_ptr (skb->data); +struct in6_addr *match_prefix; +struct ipv6hdr *ip6h; +struct ethhdr *eth; + + match_prefix = (struct in6_addr *)ipv6_prefix; + +if (skb->protocol !=bpf_htons (ETH_P_IPV6 )) +return TC_ACT_OK ; + + eth = data; +if ((void *)(eth +1 ) > data_end) +return TC_ACT_OK ; + + ip6h = data +sizeof (struct ethhdr ); +if ((void *)(ip6h +1 ) > data_end) +return TC_ACT_OK ; + +if (!v6_p64_equal (ip6h->daddr, (*match_prefix))) +return TC_ACT_OK ; + +/* +* Use bpf_redirect_neigh() to perform neighbor lookup and fill in +* correct L2 addresses for the target interface. +*/ +return bpf_redirect_neigh (redirect_ifindex,NULL ,0 ,0 ); +} + +char __license[] SEC("license") = "GPL";
Signature · 1 line
2.52.0
PATCH v11 5/5 selftest/net: psp: Add test for dev-assoc/disassoc
In reply to Wei WangFrom: Wei Wang <[email protected]>
Add a new param to NetDrvContEnv to add an additional bpf redirect program on nk_host to redirect traffic to the psp_dev_local. The topology looks like this:
Host NS: psp_dev_local <---> nk_host
| |
| | (netkit pair)
| |
Remote NS: psp_dev_peer Guest NS: nk_guest
(responder) (PSP tests)Add following tests for dev-assoc/dev-disassoc functionality:
1. Test the output of ./tools/net/ynl/pyynl/cli.py --spec Documentation/netlink/specs/psp.yaml --dump dev-get in both default and the guest netns.
2. Test the case where we associate netkit with psp_dev_local, and send PSP traffic from nk_guest to psp_dev_peer in 2 different netns.
3. Test to make sure the key rotation notification is sent to the netns for associated dev as well
4. Test to make sure the dev change notification is sent to the netns for associated dev as well
5. Test for dev-assoc/dev-disassoc without nsid parameter.
6. Test the deletion of nk_guest in client netns, and proper cleanup in the assoc-list for psp dev.
--- tools/testing/selftests/drivers/net/config | 1 + .../selftests/drivers/net/lib/py/env.py | 54 ++- tools/testing/selftests/drivers/net/psp.py | 457 ++++++++++++++++-- 3 files changed, 478 insertions(+), 34 deletions(-)
tools/testing/selftests/drivers/net/config +1 -0
@@ -7,4 +7,5 @@ CONFIG_NETCONSOLE=m CONFIG_NETCONSOLE_DYNAMIC=y CONFIG_NETCONSOLE_EXTENDED_LOG=y CONFIG_NETDEVSIM=m +CONFIG_NETKIT=y CONFIG_XDP_SOCKETS=y
tools/testing/selftests/drivers/net/lib/py/env.py +53 -1
82 lines not on this page · raw message
tools/testing/selftests/drivers/net/psp.py +424 -33
506 lines not on this page · raw message
Signature · 1 line
2.52.0