Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netlink: add netlink_kernel_cfg parameter to netlink_kernel_create

This patch adds the following structure:

struct netlink_kernel_cfg {
unsigned int groups;
void (*input)(struct sk_buff *skb);
struct mutex *cb_mutex;
};

That can be passed to netlink_kernel_create to set optional configurations
for netlink kernel sockets.

I've populated this structure by looking for NULL and zero parameters at the
existing code. The remaining parameters that always need to be set are still
left in the original interface.

That includes optional parameters for the netlink socket creation. This allows
easy extensibility of this interface in the future.

This patch also adapts all callers to use this new interface.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Pablo Neira Ayuso and committed by
David S. Miller
a31f2d17 dd7f36ba

+117 -51
+5 -2
crypto/crypto_user.c
··· 496 496 497 497 static int __init crypto_user_init(void) 498 498 { 499 + struct netlink_kernel_cfg cfg = { 500 + .input = crypto_netlink_rcv, 501 + }; 502 + 499 503 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, 500 - 0, crypto_netlink_rcv, 501 - NULL, THIS_MODULE); 504 + THIS_MODULE, &cfg); 502 505 if (!crypto_nlsk) 503 506 return -ENOMEM; 504 507
+9 -4
drivers/connector/connector.c
··· 251 251 .release = single_release 252 252 }; 253 253 254 + static struct cn_dev cdev = { 255 + .input = cn_rx_skb, 256 + }; 257 + 254 258 static int __devinit cn_init(void) 255 259 { 256 260 struct cn_dev *dev = &cdev; 257 - 258 - dev->input = cn_rx_skb; 261 + struct netlink_kernel_cfg cfg = { 262 + .groups = CN_NETLINK_USERS + 0xf, 263 + .input = dev->input, 264 + }; 259 265 260 266 dev->nls = netlink_kernel_create(&init_net, NETLINK_CONNECTOR, 261 - CN_NETLINK_USERS + 0xf, 262 - dev->input, NULL, THIS_MODULE); 267 + THIS_MODULE, &cfg); 263 268 if (!dev->nls) 264 269 return -EIO; 265 270
+5 -2
drivers/infiniband/core/netlink.c
··· 173 173 174 174 int __init ibnl_init(void) 175 175 { 176 - nls = netlink_kernel_create(&init_net, NETLINK_RDMA, 0, ibnl_rcv, 177 - NULL, THIS_MODULE); 176 + struct netlink_kernel_cfg cfg = { 177 + .input = ibnl_rcv, 178 + }; 179 + 180 + nls = netlink_kernel_create(&init_net, NETLINK_RDMA, THIS_MODULE, &cfg); 178 181 if (!nls) { 179 182 pr_warn("Failed to create netlink socket\n"); 180 183 return -ENOMEM;
+5 -2
drivers/scsi/scsi_netlink.c
··· 486 486 scsi_netlink_init(void) 487 487 { 488 488 int error; 489 + struct netlink_kernel_cfg cfg = { 490 + .input = scsi_nl_rcv_msg, 491 + .groups = SCSI_NL_GRP_CNT, 492 + }; 489 493 490 494 INIT_LIST_HEAD(&scsi_nl_drivers); 491 495 ··· 501 497 } 502 498 503 499 scsi_nl_sock = netlink_kernel_create(&init_net, NETLINK_SCSITRANSPORT, 504 - SCSI_NL_GRP_CNT, scsi_nl_rcv_msg, NULL, 505 - THIS_MODULE); 500 + THIS_MODULE, &cfg); 506 501 if (!scsi_nl_sock) { 507 502 printk(KERN_ERR "%s: register of receive handler failed\n", 508 503 __func__);
+6 -3
drivers/scsi/scsi_transport_iscsi.c
··· 2936 2936 static __init int iscsi_transport_init(void) 2937 2937 { 2938 2938 int err; 2939 - 2939 + struct netlink_kernel_cfg cfg = { 2940 + .groups = 1, 2941 + .input = iscsi_if_rx, 2942 + }; 2940 2943 printk(KERN_INFO "Loading iSCSI transport class v%s.\n", 2941 2944 ISCSI_TRANSPORT_VERSION); 2942 2945 ··· 2969 2966 if (err) 2970 2967 goto unregister_conn_class; 2971 2968 2972 - nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 1, iscsi_if_rx, 2973 - NULL, THIS_MODULE); 2969 + nls = netlink_kernel_create(&init_net, NETLINK_ISCSI, 2970 + THIS_MODULE, &cfg); 2974 2971 if (!nls) { 2975 2972 err = -ENOBUFS; 2976 2973 goto unregister_session_class;
+10 -5
include/linux/netlink.h
··· 174 174 extern void netlink_table_grab(void); 175 175 extern void netlink_table_ungrab(void); 176 176 177 - extern struct sock *netlink_kernel_create(struct net *net, 178 - int unit,unsigned int groups, 179 - void (*input)(struct sk_buff *skb), 180 - struct mutex *cb_mutex, 181 - struct module *module); 177 + /* optional Netlink kernel configuration parameters */ 178 + struct netlink_kernel_cfg { 179 + unsigned int groups; 180 + void (*input)(struct sk_buff *skb); 181 + struct mutex *cb_mutex; 182 + }; 183 + 184 + extern struct sock *netlink_kernel_create(struct net *net, int unit, 185 + struct module *module, 186 + struct netlink_kernel_cfg *cfg); 182 187 extern void netlink_kernel_release(struct sock *sk); 183 188 extern int __netlink_change_ngroups(struct sock *sk, unsigned int groups); 184 189 extern int netlink_change_ngroups(struct sock *sk, unsigned int groups);
+5 -2
kernel/audit.c
··· 962 962 static int __init audit_init(void) 963 963 { 964 964 int i; 965 + struct netlink_kernel_cfg cfg = { 966 + .input = audit_receive, 967 + }; 965 968 966 969 if (audit_initialized == AUDIT_DISABLED) 967 970 return 0; 968 971 969 972 printk(KERN_INFO "audit: initializing netlink socket (%s)\n", 970 973 audit_default ? "enabled" : "disabled"); 971 - audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, 0, 972 - audit_receive, NULL, THIS_MODULE); 974 + audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, 975 + THIS_MODULE, &cfg); 973 976 if (!audit_sock) 974 977 audit_panic("cannot initialize netlink socket"); 975 978 else
+4 -1
lib/kobject_uevent.c
··· 373 373 static int uevent_net_init(struct net *net) 374 374 { 375 375 struct uevent_sock *ue_sk; 376 + struct netlink_kernel_cfg cfg = { 377 + .groups = 1, 378 + }; 376 379 377 380 ue_sk = kzalloc(sizeof(*ue_sk), GFP_KERNEL); 378 381 if (!ue_sk) 379 382 return -ENOMEM; 380 383 381 384 ue_sk->sk = netlink_kernel_create(net, NETLINK_KOBJECT_UEVENT, 382 - 1, NULL, NULL, THIS_MODULE); 385 + THIS_MODULE, &cfg); 383 386 if (!ue_sk->sk) { 384 387 printk(KERN_ERR 385 388 "kobject_uevent: unable to create netlink socket!\n");
+4 -2
net/bridge/netfilter/ebt_ulog.c
··· 282 282 { 283 283 int ret; 284 284 int i; 285 + struct netlink_kernel_cfg cfg = { 286 + .groups = EBT_ULOG_MAXNLGROUPS, 287 + }; 285 288 286 289 if (nlbufsiz >= 128*1024) { 287 290 pr_warning("Netlink buffer has to be <= 128kB," ··· 299 296 } 300 297 301 298 ebtulognl = netlink_kernel_create(&init_net, NETLINK_NFLOG, 302 - EBT_ULOG_MAXNLGROUPS, NULL, NULL, 303 - THIS_MODULE); 299 + THIS_MODULE, &cfg); 304 300 if (!ebtulognl) 305 301 ret = -ENOMEM; 306 302 else if ((ret = xt_register_target(&ebt_ulog_tg_reg)) != 0)
+7 -2
net/core/rtnetlink.c
··· 2353 2353 static int __net_init rtnetlink_net_init(struct net *net) 2354 2354 { 2355 2355 struct sock *sk; 2356 - sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX, 2357 - rtnetlink_rcv, &rtnl_mutex, THIS_MODULE); 2356 + struct netlink_kernel_cfg cfg = { 2357 + .groups = RTNLGRP_MAX, 2358 + .input = rtnetlink_rcv, 2359 + .cb_mutex = &rtnl_mutex, 2360 + }; 2361 + 2362 + sk = netlink_kernel_create(net, NETLINK_ROUTE, THIS_MODULE, &cfg); 2358 2363 if (!sk) 2359 2364 return -ENOMEM; 2360 2365 net->rtnl = sk;
+6 -2
net/core/sock_diag.c
··· 171 171 172 172 static int __init sock_diag_init(void) 173 173 { 174 - sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0, 175 - sock_diag_rcv, NULL, THIS_MODULE); 174 + struct netlink_kernel_cfg cfg = { 175 + .input = sock_diag_rcv, 176 + }; 177 + 178 + sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 179 + THIS_MODULE, &cfg); 176 180 return sock_diag_nlsk == NULL ? -ENOMEM : 0; 177 181 } 178 182
+5 -3
net/decnet/netfilter/dn_rtmsg.c
··· 125 125 static int __init dn_rtmsg_init(void) 126 126 { 127 127 int rv = 0; 128 + struct netlink_kernel_cfg cfg = { 129 + .groups = DNRNG_NLGRP_MAX, 130 + .input = dnrmg_receive_user_skb, 131 + }; 128 132 129 133 dnrmg = netlink_kernel_create(&init_net, 130 - NETLINK_DNRTMSG, DNRNG_NLGRP_MAX, 131 - dnrmg_receive_user_skb, 132 - NULL, THIS_MODULE); 134 + NETLINK_DNRTMSG, THIS_MODULE, &cfg); 133 135 if (dnrmg == NULL) { 134 136 printk(KERN_ERR "dn_rtmsg: Cannot create netlink socket"); 135 137 return -ENOMEM;
+5 -2
net/ipv4/fib_frontend.c
··· 976 976 static int __net_init nl_fib_lookup_init(struct net *net) 977 977 { 978 978 struct sock *sk; 979 - sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0, 980 - nl_fib_input, NULL, THIS_MODULE); 979 + struct netlink_kernel_cfg cfg = { 980 + .input = nl_fib_input, 981 + }; 982 + 983 + sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, THIS_MODULE, &cfg); 981 984 if (sk == NULL) 982 985 return -EAFNOSUPPORT; 983 986 net->ipv4.fibnl = sk;
+5 -3
net/ipv4/netfilter/ipt_ULOG.c
··· 381 381 static int __init ulog_tg_init(void) 382 382 { 383 383 int ret, i; 384 + struct netlink_kernel_cfg cfg = { 385 + .groups = ULOG_MAXNLGROUPS, 386 + }; 384 387 385 388 pr_debug("init module\n"); 386 389 ··· 396 393 for (i = 0; i < ULOG_MAXNLGROUPS; i++) 397 394 setup_timer(&ulog_buffers[i].timer, ulog_timer, i); 398 395 399 - nflognl = netlink_kernel_create(&init_net, 400 - NETLINK_NFLOG, ULOG_MAXNLGROUPS, NULL, 401 - NULL, THIS_MODULE); 396 + nflognl = netlink_kernel_create(&init_net, NETLINK_NFLOG, 397 + THIS_MODULE, &cfg); 402 398 if (!nflognl) 403 399 return -ENOMEM; 404 400
+5 -2
net/netfilter/nfnetlink.c
··· 203 203 static int __net_init nfnetlink_net_init(struct net *net) 204 204 { 205 205 struct sock *nfnl; 206 + struct netlink_kernel_cfg cfg = { 207 + .groups = NFNLGRP_MAX, 208 + .input = nfnetlink_rcv, 209 + }; 206 210 207 - nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, NFNLGRP_MAX, 208 - nfnetlink_rcv, NULL, THIS_MODULE); 211 + nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, THIS_MODULE, &cfg); 209 212 if (!nfnl) 210 213 return -ENOMEM; 211 214 net->nfnl_stash = nfnl;
+10 -6
net/netlink/af_netlink.c
··· 1503 1503 */ 1504 1504 1505 1505 struct sock * 1506 - netlink_kernel_create(struct net *net, int unit, unsigned int groups, 1507 - void (*input)(struct sk_buff *skb), 1508 - struct mutex *cb_mutex, struct module *module) 1506 + netlink_kernel_create(struct net *net, int unit, 1507 + struct module *module, 1508 + struct netlink_kernel_cfg *cfg) 1509 1509 { 1510 1510 struct socket *sock; 1511 1511 struct sock *sk; 1512 1512 struct netlink_sock *nlk; 1513 1513 struct listeners *listeners = NULL; 1514 + struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL; 1515 + unsigned int groups; 1514 1516 1515 1517 BUG_ON(!nl_table); 1516 1518 ··· 1534 1532 sk = sock->sk; 1535 1533 sk_change_net(sk, net); 1536 1534 1537 - if (groups < 32) 1535 + if (!cfg || cfg->groups < 32) 1538 1536 groups = 32; 1537 + else 1538 + groups = cfg->groups; 1539 1539 1540 1540 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); 1541 1541 if (!listeners) 1542 1542 goto out_sock_release; 1543 1543 1544 1544 sk->sk_data_ready = netlink_data_ready; 1545 - if (input) 1546 - nlk_sk(sk)->netlink_rcv = input; 1545 + if (cfg && cfg->input) 1546 + nlk_sk(sk)->netlink_rcv = cfg->input; 1547 1547 1548 1548 if (netlink_insert(sk, net, 0)) 1549 1549 goto out_sock_release;
+7 -3
net/netlink/genetlink.c
··· 915 915 916 916 static int __net_init genl_pernet_init(struct net *net) 917 917 { 918 + struct netlink_kernel_cfg cfg = { 919 + .input = genl_rcv, 920 + .cb_mutex = &genl_mutex, 921 + }; 922 + 918 923 /* we'll bump the group number right afterwards */ 919 - net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 0, 920 - genl_rcv, &genl_mutex, 921 - THIS_MODULE); 924 + net->genl_sock = netlink_kernel_create(net, NETLINK_GENERIC, 925 + THIS_MODULE, &cfg); 922 926 923 927 if (!net->genl_sock && net_eq(net, &init_net)) 924 928 panic("GENL: Cannot initialize generic netlink\n");
+5 -2
net/xfrm/xfrm_user.c
··· 2959 2959 static int __net_init xfrm_user_net_init(struct net *net) 2960 2960 { 2961 2961 struct sock *nlsk; 2962 + struct netlink_kernel_cfg cfg = { 2963 + .groups = XFRMNLGRP_MAX, 2964 + .input = xfrm_netlink_rcv, 2965 + }; 2962 2966 2963 - nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX, 2964 - xfrm_netlink_rcv, NULL, THIS_MODULE); 2967 + nlsk = netlink_kernel_create(net, NETLINK_XFRM, THIS_MODULE, &cfg); 2965 2968 if (nlsk == NULL) 2966 2969 return -ENOMEM; 2967 2970 net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
+5 -1
security/selinux/netlink.c
··· 111 111 112 112 static int __init selnl_init(void) 113 113 { 114 + struct netlink_kernel_cfg cfg = { 115 + .groups = SELNLGRP_MAX, 116 + }; 117 + 114 118 selnl = netlink_kernel_create(&init_net, NETLINK_SELINUX, 115 - SELNLGRP_MAX, NULL, NULL, THIS_MODULE); 119 + THIS_MODULE, &cfg); 116 120 if (selnl == NULL) 117 121 panic("SELinux: Cannot create netlink socket."); 118 122 netlink_set_nonroot(NETLINK_SELINUX, NL_NONROOT_RECV);