History

sunrpc: add netlink upcall for the auth.unix.ip cache

Add netlink-based cache upcall support for the ip_map (auth.unix.ip) cache, using the sunrpc generic netlink family. Add ip-map attribute-set (seqno, class, addr, domain, negative, expiry), ip-map-reqs wrapper, and ip-map-get-reqs / ip-map-set-reqs operations to the sunrpc_cache YAML spec and generated headers. Implement sunrpc_nl_ip_map_get_reqs_dumpit() which snapshots pending ip_map cache requests and sends each entry's seqno, class name, and IP address over netlink. Implement sunrpc_nl_ip_map_set_reqs_doit() which parses ip_map cache responses from userspace (class, addr, expiry, and domain name or negative flag) and updates the cache via __ip_map_lookup() / __ip_map_update(). Wire up ip_map_notify() callback in ip_map_cache_template so cache misses trigger SUNRPC_CMD_CACHE_NOTIFY multicast events with SUNRPC_CACHE_TYPE_IP_MAP.

Jeff Layton committed 7 months ago

Commit 712bdbb1 parent af81cb25 files · +356 −0Browse files

Unified · Split

5 files
  1. Documentation/netlink/specs/sunrpc_cache.yaml +47 −0
  2. include/uapi/linux/sunrpc_netlink.h +21 −0
  3. net/sunrpc/netlink.c +34 −0
  4. net/sunrpc/netlink.h +8 −0
  5. net/sunrpc/svcauth_unix.c +246 −0
Documentation/netlink/specs/sunrpc_cache.yaml+47−0
@@ -20,6 +20,35 @@ attribute-sets: · 19 unmodified lines
2020 name: cache-type
2121 type: u32
2222 enum: cache-type
23+ -
24+ name: ip-map
25+ attributes:
26+ -
27+ name: seqno
28+ type: u64
29+ -
30+ name: class
31+ type: string
32+ -
33+ name: addr
34+ type: string
35+ -
36+ name: domain
37+ type: string
38+ -
39+ name: negative
40+ type: flag
41+ -
42+ name: expiry
43+ type: u64
44+ -
45+ name: ip-map-reqs
46+ attributes:
47+ -
48+ name: requests
49+ type: nest
50+ nested-attributes: ip-map
51+ multi-attr: true
2352
2453 operations:
2554 list:
@@ -31,6 +60,24 @@ operations: · 5 unmodified lines
3160 event:
3261 attributes:
3362 - cache-type
63+ -
64+ name: ip-map-get-reqs
65+ doc: Dump all pending ip_map requests
66+ attribute-set: ip-map-reqs
67+ flags: [admin-perm]
68+ dump:
69+ request:
70+ attributes:
71+ - requests
72+ -
73+ name: ip-map-set-reqs
74+ doc: Respond to one or more ip_map requests
75+ attribute-set: ip-map-reqs
76+ flags: [admin-perm]
77+ do:
78+ request:
79+ attributes:
80+ - requests
3481
3582 mcast-groups:
3683 list:
include/uapi/linux/sunrpc_netlink.h+21−0
@@ -22,8 +22,29 @@ enum { · 21 unmodified lines
2222 SUNRPC_A_CACHE_NOTIFY_MAX = (__SUNRPC_A_CACHE_NOTIFY_MAX - 1)
2323 };
2424
25+enum {
26+ SUNRPC_A_IP_MAP_SEQNO = 1,
27+ SUNRPC_A_IP_MAP_CLASS,
28+ SUNRPC_A_IP_MAP_ADDR,
29+ SUNRPC_A_IP_MAP_DOMAIN,
30+ SUNRPC_A_IP_MAP_NEGATIVE,
31+ SUNRPC_A_IP_MAP_EXPIRY,
32+
33+ __SUNRPC_A_IP_MAP_MAX,
34+ SUNRPC_A_IP_MAP_MAX = (__SUNRPC_A_IP_MAP_MAX - 1)
35+};
36+
37+enum {
38+ SUNRPC_A_IP_MAP_REQS_REQUESTS = 1,
39+
40+ __SUNRPC_A_IP_MAP_REQS_MAX,
41+ SUNRPC_A_IP_MAP_REQS_MAX = (__SUNRPC_A_IP_MAP_REQS_MAX - 1)
42+};
43+
2544 enum {
2645 SUNRPC_CMD_CACHE_NOTIFY = 1,
46+ SUNRPC_CMD_IP_MAP_GET_REQS,
47+ SUNRPC_CMD_IP_MAP_SET_REQS,
2748
2849 __SUNRPC_CMD_MAX,
2950 SUNRPC_CMD_MAX = (__SUNRPC_CMD_MAX - 1)
net/sunrpc/netlink.c+34−0
@@ -12,8 +12,42 @@ · 11 unmodified lines
1212
1313 #include <uapi/linux/sunrpc_netlink.h>
1414
15+/* Common nested types */
16+const struct nla_policy sunrpc_ip_map_nl_policy[SUNRPC_A_IP_MAP_EXPIRY + 1] = {
17+ [SUNRPC_A_IP_MAP_SEQNO] = { .type = NLA_U64, },
18+ [SUNRPC_A_IP_MAP_CLASS] = { .type = NLA_NUL_STRING, },
19+ [SUNRPC_A_IP_MAP_ADDR] = { .type = NLA_NUL_STRING, },
20+ [SUNRPC_A_IP_MAP_DOMAIN] = { .type = NLA_NUL_STRING, },
21+ [SUNRPC_A_IP_MAP_NEGATIVE] = { .type = NLA_FLAG, },
22+ [SUNRPC_A_IP_MAP_EXPIRY] = { .type = NLA_U64, },
23+};
24+
25+/* SUNRPC_CMD_IP_MAP_GET_REQS - dump */
26+static const struct nla_policy sunrpc_ip_map_get_reqs_nl_policy[SUNRPC_A_IP_MAP_REQS_REQUESTS + 1] = {
27+ [SUNRPC_A_IP_MAP_REQS_REQUESTS] = NLA_POLICY_NESTED(sunrpc_ip_map_nl_policy),
28+};
29+
30+/* SUNRPC_CMD_IP_MAP_SET_REQS - do */
31+static const struct nla_policy sunrpc_ip_map_set_reqs_nl_policy[SUNRPC_A_IP_MAP_REQS_REQUESTS + 1] = {
32+ [SUNRPC_A_IP_MAP_REQS_REQUESTS] = NLA_POLICY_NESTED(sunrpc_ip_map_nl_policy),
33+};
34+
1535 /* Ops table for sunrpc */
1636 static const struct genl_split_ops sunrpc_nl_ops[] = {
37+ {
38+ .cmd = SUNRPC_CMD_IP_MAP_GET_REQS,
39+ .dumpit = sunrpc_nl_ip_map_get_reqs_dumpit,
40+ .policy = sunrpc_ip_map_get_reqs_nl_policy,
41+ .maxattr = SUNRPC_A_IP_MAP_REQS_REQUESTS,
42+ .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DUMP,
43+ },
44+ {
45+ .cmd = SUNRPC_CMD_IP_MAP_SET_REQS,
46+ .doit = sunrpc_nl_ip_map_set_reqs_doit,
47+ .policy = sunrpc_ip_map_set_reqs_nl_policy,
48+ .maxattr = SUNRPC_A_IP_MAP_REQS_REQUESTS,
49+ .flags = GENL_ADMIN_PERM | GENL_CMD_CAP_DO,
50+ },
1751 };
1852
1953 static const struct genl_multicast_group sunrpc_nl_mcgrps[] = {
net/sunrpc/netlink.h+8−0
@@ -12,6 +12,14 @@ · 11 unmodified lines
1212
1313 #include <uapi/linux/sunrpc_netlink.h>
1414
15+/* Common nested types */
16+extern const struct nla_policy sunrpc_ip_map_nl_policy[SUNRPC_A_IP_MAP_EXPIRY + 1];
17+
18+int sunrpc_nl_ip_map_get_reqs_dumpit(struct sk_buff *skb,
19+ struct netlink_callback *cb);
20+int sunrpc_nl_ip_map_set_reqs_doit(struct sk_buff *skb,
21+ struct genl_info *info);
22+
1523 enum {
1624 SUNRPC_NLGRP_NONE,
1725 SUNRPC_NLGRP_EXPORTD,
net/sunrpc/svcauth_unix.c+246−0
@@ -17,11 +17,14 @@ · 16 unmodified lines
1717 #include <net/ipv6.h>
1818 #include <linux/kernel.h>
1919 #include <linux/user_namespace.h>
20+#include <net/genetlink.h>
21+#include <uapi/linux/sunrpc_netlink.h>
2022 #include <trace/events/sunrpc.h>
2123
2224 #define RPCDBG_FACILITY RPCDBG_AUTH
2325
2426 #include "netns.h"
27+#include "netlink.h"
2528
2629 /*
2730 * AUTHUNIX and AUTHNULL credentials are both handled here.
@@ -1017,12 +1020,255 @@ struct auth_ops svcauth_unix = { · 989 unmodified lines
10171020 .set_client = svcauth_unix_set_client,
10181021 };
10191022
1023+static int ip_map_notify(struct cache_detail *cd, struct cache_head *h)
1024+{
1025+ return sunrpc_cache_notify(cd, h, SUNRPC_CACHE_TYPE_IP_MAP);
1026+}
1027+
1028+/**
1029+ * sunrpc_nl_ip_map_get_reqs_dumpit - dump pending ip_map requests
1030+ * @skb: reply buffer
1031+ * @cb: netlink metadata and command arguments
1032+ *
1033+ * Walk the ip_map cache's pending request list and create a netlink
1034+ * message with a nested entry for each cache_request, containing the
1035+ * seqno, class and addr.
1036+ *
1037+ * Uses cb->args[0] as a seqno cursor for dump continuation across
1038+ * multiple netlink messages.
1039+ *
1040+ * Returns the size of the reply or a negative errno.
1041+ */
1042+int sunrpc_nl_ip_map_get_reqs_dumpit(struct sk_buff *skb,
1043+ struct netlink_callback *cb)
1044+{
1045+ struct sunrpc_net *sn;
1046+ struct cache_detail *cd;
1047+ struct cache_head **items;
1048+ u64 *seqnos;
1049+ int cnt, i, emitted;
1050+ void *hdr;
1051+ int ret;
1052+
1053+ sn = net_generic(sock_net(skb->sk), sunrpc_net_id);
1054+
1055+ cd = sn->ip_map_cache;
1056+ if (!cd)
1057+ return -ENODEV;
1058+
1059+ cnt = sunrpc_cache_requests_count(cd);
1060+ if (!cnt)
1061+ return 0;
1062+
1063+ items = kcalloc(cnt, sizeof(*items), GFP_KERNEL);
1064+ seqnos = kcalloc(cnt, sizeof(*seqnos), GFP_KERNEL);
1065+ if (!items || !seqnos) {
1066+ ret = -ENOMEM;
1067+ goto out_alloc;
1068+ }
1069+
1070+ cnt = sunrpc_cache_requests_snapshot(cd, items, seqnos, cnt,
1071+ cb->args[0]);
1072+ if (!cnt) {
1073+ ret = 0;
1074+ goto out_alloc;
1075+ }
1076+
1077+ hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid,
1078+ cb->nlh->nlmsg_seq, &sunrpc_nl_family,
1079+ NLM_F_MULTI, SUNRPC_CMD_IP_MAP_GET_REQS);
1080+ if (!hdr) {
1081+ ret = -ENOBUFS;
1082+ goto out_put;
1083+ }
1084+
1085+ emitted = 0;
1086+ for (i = 0; i < cnt; i++) {
1087+ struct ip_map *im;
1088+ struct nlattr *nest;
1089+ char text_addr[40];
1090+
1091+ im = container_of(items[i], struct ip_map, h);
1092+
1093+ if (ipv6_addr_v4mapped(&im->m_addr))
1094+ snprintf(text_addr, 20, "%pI4",
1095+ &im->m_addr.s6_addr32[3]);
1096+ else
1097+ snprintf(text_addr, 40, "%pI6", &im->m_addr);
1098+
1099+ nest = nla_nest_start(skb, SUNRPC_A_IP_MAP_REQS_REQUESTS);
1100+ if (!nest)
1101+ break;
1102+
1103+ if (nla_put_u64_64bit(skb, SUNRPC_A_IP_MAP_SEQNO,
1104+ seqnos[i], 0) ||
1105+ nla_put_string(skb, SUNRPC_A_IP_MAP_CLASS,
1106+ im->m_class) ||
1107+ nla_put_string(skb, SUNRPC_A_IP_MAP_ADDR, text_addr)) {
1108+ nla_nest_cancel(skb, nest);
1109+ break;
1110+ }
1111+
1112+ nla_nest_end(skb, nest);
1113+ cb->args[0] = seqnos[i];
1114+ emitted++;
1115+ }
1116+
1117+ if (!emitted) {
1118+ genlmsg_cancel(skb, hdr);
1119+ ret = -EMSGSIZE;
1120+ goto out_put;
1121+ }
1122+
1123+ genlmsg_end(skb, hdr);
1124+ ret = skb->len;
1125+out_put:
1126+ for (i = 0; i < cnt; i++)
1127+ cache_put(items[i], cd);
1128+out_alloc:
1129+ kfree(seqnos);
1130+ kfree(items);
1131+ return ret;
1132+}
1133+
1134+/**
1135+ * sunrpc_nl_parse_one_ip_map - parse one ip_map entry from netlink
1136+ * @cd: cache_detail for the ip_map cache
1137+ * @attr: nested attribute containing ip_map fields
1138+ *
1139+ * Parses one ip_map entry from a netlink message and updates the
1140+ * cache. Mirrors the logic in ip_map_parse().
1141+ *
1142+ * Returns 0 on success or a negative errno.
1143+ */
1144+static int sunrpc_nl_parse_one_ip_map(struct cache_detail *cd,
1145+ struct nlattr *attr)
1146+{
1147+ struct nlattr *tb[SUNRPC_A_IP_MAP_EXPIRY + 1];
1148+ union {
1149+ struct sockaddr sa;
1150+ struct sockaddr_in s4;
1151+ struct sockaddr_in6 s6;
1152+ } address;
1153+ struct sockaddr_in6 sin6;
1154+ struct ip_map *ipmp;
1155+ struct auth_domain *dom = NULL;
1156+ struct unix_domain *udom = NULL;
1157+ struct timespec64 boot;
1158+ time64_t expiry;
1159+ char class[8];
1160+ int err;
1161+ int len;
1162+
1163+ err = nla_parse_nested(tb, SUNRPC_A_IP_MAP_EXPIRY, attr,
1164+ sunrpc_ip_map_nl_policy, NULL);
1165+ if (err)
1166+ return err;
1167+
1168+ /* class (required) */
1169+ if (!tb[SUNRPC_A_IP_MAP_CLASS])
1170+ return -EINVAL;
1171+ len = nla_len(tb[SUNRPC_A_IP_MAP_CLASS]);
1172+ if (len <= 0 || len > sizeof(class))
1173+ return -EINVAL;
1174+ nla_strscpy(class, tb[SUNRPC_A_IP_MAP_CLASS], sizeof(class));
1175+
1176+ /* addr (required) */
1177+ if (!tb[SUNRPC_A_IP_MAP_ADDR])
1178+ return -EINVAL;
1179+ if (rpc_pton(cd->net, nla_data(tb[SUNRPC_A_IP_MAP_ADDR]),
1180+ nla_len(tb[SUNRPC_A_IP_MAP_ADDR]) - 1,
1181+ &address.sa, sizeof(address)) == 0)
1182+ return -EINVAL;
1183+
1184+ switch (address.sa.sa_family) {
1185+ case AF_INET:
1186+ sin6.sin6_family = AF_INET6;
1187+ ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
1188+ &sin6.sin6_addr);
1189+ break;
1190+#if IS_ENABLED(CONFIG_IPV6)
1191+ case AF_INET6:
1192+ memcpy(&sin6, &address.s6, sizeof(sin6));
1193+ break;
1194+#endif
1195+ default:
1196+ return -EINVAL;
1197+ }
1198+
1199+ /* expiry (required, wallclock seconds) */
1200+ if (!tb[SUNRPC_A_IP_MAP_EXPIRY])
1201+ return -EINVAL;
1202+ getboottime64(&boot);
1203+ expiry = nla_get_u64(tb[SUNRPC_A_IP_MAP_EXPIRY]) - boot.tv_sec;
1204+
1205+ /* domain name or negative */
1206+ if (tb[SUNRPC_A_IP_MAP_NEGATIVE]) {
1207+ udom = NULL;
1208+ } else if (tb[SUNRPC_A_IP_MAP_DOMAIN]) {
1209+ dom = unix_domain_find(nla_data(tb[SUNRPC_A_IP_MAP_DOMAIN]));
1210+ if (!dom)
1211+ return -ENOENT;
1212+ udom = container_of(dom, struct unix_domain, h);
1213+ } else {
1214+ return -EINVAL;
1215+ }
1216+
1217+ ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
1218+ if (ipmp)
1219+ err = __ip_map_update(cd, ipmp, udom, expiry);
1220+ else
1221+ err = -ENOMEM;
1222+
1223+ if (dom)
1224+ auth_domain_put(dom);
1225+
1226+ cache_flush();
1227+ return err;
1228+}
1229+
1230+/**
1231+ * sunrpc_nl_ip_map_set_reqs_doit - respond to ip_map requests
1232+ * @skb: reply buffer
1233+ * @info: netlink metadata and command arguments
1234+ *
1235+ * Parse one or more ip_map cache responses from userspace and
1236+ * update the ip_map cache accordingly.
1237+ *
1238+ * Returns 0 on success or a negative errno.
1239+ */
1240+int sunrpc_nl_ip_map_set_reqs_doit(struct sk_buff *skb,
1241+ struct genl_info *info)
1242+{
1243+ struct sunrpc_net *sn;
1244+ struct cache_detail *cd;
1245+ const struct nlattr *attr;
1246+ int rem, ret = 0;
1247+
1248+ sn = net_generic(genl_info_net(info), sunrpc_net_id);
1249+
1250+ cd = sn->ip_map_cache;
1251+ if (!cd)
1252+ return -ENODEV;
1253+
1254+ nlmsg_for_each_attr_type(attr, SUNRPC_A_IP_MAP_REQS_REQUESTS,
1255+ info->nlhdr, GENL_HDRLEN, rem) {
1256+ ret = sunrpc_nl_parse_one_ip_map(cd,
1257+ (struct nlattr *)attr);
1258+ if (ret)
1259+ break;
1260+ }
1261+
1262+ return ret;
1263+}
1264+
10201265 static const struct cache_detail ip_map_cache_template = {
10211266 .owner = THIS_MODULE,
10221267 .hash_size = IP_HASHMAX,
10231268 .name = "auth.unix.ip",
10241269 .cache_put = ip_map_put,
10251270 .cache_upcall = ip_map_upcall,
1271+ .cache_notify = ip_map_notify,
10261272 .cache_request = ip_map_request,
10271273 .cache_parse = ip_map_parse,
10281274 .cache_show = ip_map_show,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t