Network interface layer in Linux kernel

Dear all,

lets say a linux based computer(debian) with ethernet,wifi, usb modem connected to the, linux kernel version is 3.3

Question:

  1. for each network device the network driver builds its own net_device struct and register itself with the network interface layer of the kernel.

    text struct net_device net_dev[3] = { {init: ethernet_init,}, {init: wifi_init,}, {init: usbnet_init,}, }; register_netdev(net_dev[0]); register_netdev(net_dev[1]); register_netdev(net_dev[2]);


    so in total there are 3 network devices as seen by the kernel
    now how does the kernel decide to which network interface. to use while forwarding IP packet.

    can some one please specify any link or document which answers the above question ?

Hello

I'm not sure that I'd understand your question correctly.
Linux will use a existing routing table and route a packet to the network interface depending from that.
You can google for 'linux route table' yourself, can't post a link, sorry.

Let's me explain you with a little example.
You can have a route table looking like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
172.16.1.0     *               255.255.255.0   U     0      0        0 wifi0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
link-local      *               255.255.0.0     U     1002   0        0 eth0
default         192.168.1.1     0.0.0.0         UG    0      0        0 eth0

So if IP packet dst field will be in 192.168.1.0/24 network, it will be forwarded to eth0, and if it will be at 172.16.1.0/24 it will be forwarded to wifi0. Other way it will be sent to default gateway.

Did I answered your question?
You can find more relevant info here.