Linux TCP/IP stack layers


STACK LAYERS

Application Layer

At application layer, what users directly manipulate is socket{} file descriptor. File system defines common interfaces for different types of files. Those interfaces in turn invoke certain system calls. Thus, any call to a particular interface of socket{} file descriptor is turned into a call to a particular funtion of BSD Soceket, which lies at the BSD Socket layer.

BSD Socket Layer

At BSD Socket layer, the manipulation target is socket{}. Every created socket{} represents one network connection, belongs to one particular network address family and chooses operations accordingly, such as deciding whether to enter INET Socket layer or not. Data at this layer are stored in msghdr{}.

INET Socket Layer

At INET Socket layer, the manipulation target is sock{}. A sock{} is either connection-oriented (TCP) or connectionless (UDP). Data at this layer are stored in sk_buff{}.

IP Layer

From INET Socket layer to IP layer lies the routing process: during sending, deciding which network device interface to use and what's the address of the next hop; during receiving, deciding whether to deliver received packets to upper layers or to act as an intermediary.

Hardware Layer

This layer mainly consists of network device interface drivers.

Network Device Interface

Here is the hardware.

 

SENDING MECHANISM

fs/read_write.c

asmlinkage ssize_t sys_write(unsigned int fd, const char *buf, size_t count)

net/socket.c

asmlinkage long sys_send(int fd, void *buff, size_t len, unsigned flags)

asmlinkage long sys_sendto(int fd, void *buff, size_t len, unsigned flags, struct sockaddr *addr, int addr_len)

static ssize_t sock_write(struct file *file, const char *ubuf, size_t size, loff_t *ppos)

int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)

net/ipv4/af_inet.c

int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size, struct scm_cookie *scm)

net/ipv4/tcp.c

int tcp_sendmsg(struct sock *sk, struct msghdr *msg, int size)

net/ipv4/tcp_output.c

void tcp_send_skb(struct sock *sk, struct sk_buff *skb, int force_queue, unsigned cur_mss)

int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb)

net/ipv4/ip_output.c

int ip_queue_xmit(struct sk_buff *skb)

 

RECEIVING MECHANISM

fs/read_write.c

asmlinkage ssize_t sys_read(unsigned int fd, char *buf, size_t count)

net/socket.c

asmlinkage long sys_recv(int fd, void *ubuf, size_t size, unsigned flags)

asmlinkage long sys_recvfrom(int fd, void *ubuf, size_t size, unsigned flags, struct sockaddr *addr, int *addr_len)

static ssize_t sock_read(struct file *file, char *ubuf, size_t size, loff_t *ppos)

int sock_redvmsg(struct socket *sock, struct msghdr *msg, int size, int flags)

net/ipv4/af_inet.c

int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)

net/ipv4/tcp.c

int tcp_recvmsg(struct sock *sk, struct msghdr *msg, int len, int nonblock, int flags, int *addr_len)

net/ipv4/tcp_ipv4.c

int tcp_v4_rcv(struct sk_buff *skb, unsigned short len)

int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)

net/ipv4/ip_input.c

int tcp_rcv_established(struct sock *sk, struct sk_buff *skb, struct tcphdr *th, unsigned len)

net/ipv4/ip_input.c

int ip_local_deliver(struct sk_buff *skb)