Linux Networking (II)
Mar 22, 2002
Yongguang Zhang
Today's Lecture
- Linux Networking: Output Chain
Multiplexing
- Socket has many "family"
- And many types
- SOCK_STREAM, SOCK_DGRAM, SOCK_RAW
- Each family can have many protocols:
- PF_INET: IPPROTO_TCP, IPPROTO_UDP, ...
How is a Socket Created?
How is a net_family Defined?
IPv4 INET Family
static int __init inet_init(void)
...
(void) sock_register(&inet_family_ops);
...
struct net_proto_family inet_family_ops = {
family: PF_INET,
create: inet_create
};
Protocols in INET Family
- Registrating
static struct inet_protosw inetsw_array[] = { ... }
static int __init inet_init(void)
...
for each q in inetsw_array[]
inet_register_protosw(q);
- Socket creating
int inet_create(struct socket *sock, int protocol)
/* Look for the requested type/protocol pair. */
sock->ops = ... ->ops; sk->prot = ... ->prot;
Example with UDP
Socket Send (and Receive)
- sys_send() calls sock_sendmsg()
- sock_sendmsg() calls
- sock->ops->sendmsg(sock, msg, size, &scm)
- Under PF_INET:
- sock->ops->sendmsg ==> inet_sendmsg()
- inet_sendmsg() calls
- sk->prot->sendmsg(sk, msg, size);
- Under IPPROTO_UDP:
- sk->prot->sendmsg ==> udp_sendmsg()
Protocol Layer
- udp_sendmsg()
- Build the UDP header, etc.
- Calls ip_build_xmit() to output the packet
IP Output
- Fast path: ip_build_xmit()
- Allocate skb (sk_buff)
- Fill in the IP header
- Get the fragment from user space
- Send out with NF hooks
- New in 2.4 kernel
- Older kernels: send to device directly
- NF: provides kernel hooks for packet manipulation, such as firewall, QoS, etc.
Next Lecture
© 2002 Yongguang Zhang