2

I have some issues using the netlink sockets with a new netlink family...in particular with groups. I use netlink_broadcast or nlmsg_multicast methods in kernel space to send messages to userspace. I have an header, for example, with the defs:

 #define NETLINK_MYFAMILY 20
 #define NL_MYGRP         2

and a process that opens a socket and binds to a netlink address:

int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_MYFAMILY);
...
struct sockaddr_nl nl_addr;
memset(&nl_addr, 0, sizeof(struct sockaddr_nl));
nl_addr.nl_family = AF_NETLINK;
nl_addr.nl_pid = getpid();
nl_addr.nl_groups = NL_MYGRP;

int r = bind(sock, (struct sockaddr *)&nl_addr, sizeof(struct sockaddr_nl));
...

obsiously in kernel space i call:

struct sock *s = netlink_kernel_create(&init_net,
                                       NETLINK_MYFAMILY,
                                       NL_MYGRP,
                                       recv_cb,
                                       NULL, THIS_MODULE);

where recv_cb is the callback called when a process sends a message from userspace.

Now i try to send a message to userspace with:

netlink_broadcast(s, skb, 0, NL_MYGRP, GFP_ATOMIC);

I can read correctly the message in the process only if the NL_MYGRP is 1. I can't figure out the problem...All netlink families are specified in linux/netlink.h and the number 20 doesn't exist so i think it could be used to specify my new family. What is wrong? Thank you all.

MirkoBanchi
  • 2,173
  • 5
  • 35
  • 52

1 Answers1

1

20 is already used by NETLINK_RDMA. Why don't you just use NETLINK_USERSOCK as your netlink family?

#define NETLINK_ROUTE       0   /* Routing/device hook              */
#define NETLINK_UNUSED      1   /* Unused number                */
#define NETLINK_USERSOCK    2   /* Reserved for user mode socket protocols  */
#define NETLINK_FIREWALL    3   /* Unused number, formerly ip_queue     */
#define NETLINK_SOCK_DIAG   4   /* socket monitoring                */
#define NETLINK_NFLOG       5   /* netfilter/iptables ULOG */
#define NETLINK_XFRM        6   /* ipsec */
#define NETLINK_SELINUX     7   /* SELinux event notifications */

#define NETLINK_ISCSI       8   /* Open-iSCSI */
#define NETLINK_AUDIT       9   /* auditing */
#define NETLINK_FIB_LOOKUP  10  
#define NETLINK_CONNECTOR   11
#define NETLINK_NETFILTER   12  /* netfilter subsystem */
#define NETLINK_IP6_FW      13
#define NETLINK_DNRTMSG     14  /* DECnet routing messages */
#define NETLINK_KOBJECT_UEVENT  15  /* Kernel messages to userspace */
#define NETLINK_GENERIC     16

/* leave room for NETLINK_DM (DM Events) */
#define NETLINK_SCSITRANSPORT   18  /* SCSI Transports */
#define NETLINK_ECRYPTFS    19
#define NETLINK_RDMA        20
#define NETLINK_CRYPTO      21  /* Crypto layer */

#define NETLINK_INET_DIAG   NETLINK_SOCK_DIAG

#define MAX_LINKS 32     
Oliver
  • 89
  • 1
  • 4
  • see here: [include/uapi/linux/netllink.h](https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/include/uapi/linux/netlink.h) – Oliver Feb 17 '16 at 21:45