next up previous contents
Next: Netlink Socket creation Up: Netlink sockets Previous: An Overview of netlink   Contents

Protocol Registration

Before actually seeing how netlink sockets are created and used, let us just take a look at how the various socket types are registered and supported by the kernel.

When the linux system comes up and memory and process management modules start working, its time to get some real work done. There is a function __init do_basic_setup() in init/main.c which essentially does all the initialisation including the sock_init(). This function in net/socket.c does the initialisation of the firewalls, protocols etc, we are interested in the protocol initialisation, there is a call to the function proto_init() which kicks off all the configured protocols. The list of protocols is maintained as a table in net/protocols.c, the section of the code is given below:

struct net_proto protocols[] = {
#ifdef  CONFIG_NETLINK
  { "NETLINK",  netlink_proto_init  },
#endif
#ifdef  CONFIG_INET
  { "INET", inet_proto_init },          /* TCP/IP */
#endif

struct net_proto
{
    const char *name;       /* Protocol name */
    void (*init_func)(struct net_proto *);  /* Bootstrap */
};

The table contains net_proto structures, every entry in the table contains the protocol name and the init function corresponding to that protocol. So the proto_init() contains the following code for the initialisation of the protocols, it picks up every entry in the table and calls the init of every protocol.

void __init proto_init(void)
{
	extern struct net_proto protocols[];    /* Network protocols */
	struct net_proto *pro;

    pro = protocols;
    while (pro->name != NULL)
    {
        (*pro->init_func)(pro);
        pro++;
    }
}

Now lets see what happens in the netlink_proto_init function.

There is yet another structure of interest,

struct net_proto_family
{
    int family;
    int (*create)(struct socket *sock, int protocol);
    /* These are counters for the number of different methods of
       each we support */
    short   authentication;
    short   encryption;
    short   encrypt_net;
};

Now for the netlink protocol, the binding between the family and the create function for the socket is given by

struct net_proto_family netlink_family_ops = {
    PF_NETLINK,
    netlink_create
};

In netlink_proto_init(), look at the following code,

void netlink_proto_init(struct net_proto *pro)
{
    .
    .
    sock_register(&netlink_family_ops);
    .
}

The sock_register() function is called by a protocol handler when it wants to advertise its address family, and have it linked to the socket module. It creates an entry for this protocol in the net_families table. The net_families contains the protocol list and all the protocols are registered here.

int sock_register(struct net_proto_family *ops)
{
    .
    .
    net_families[ops->family]=ops;
    .
}

Now that we know the basic structures, let us see what happens when a netlink socket is created in the user space. The socket() is a system call which is then resolved in the kernel, this brings us to a point where we can understand a little about how system calls work, refer to the Appendix.


next up previous contents
Next: Netlink Socket creation Up: Netlink sockets Previous: An Overview of netlink   Contents
Gowri Dhandapani
1999-10-03