TCP/IP Illustrated, Volume 2 Chapter 22 Protocol Control Blocks HUFS ICE 200530032 김영준 ddanggae@hufs.ac.kr hufs MCLAB Contents Introduction Code Introduction inpcb{} Structure in_pcballoc( ) and in_pcbdetach( ) Functions Binding, Connecting, and Demultiplexing in_pcblookup( ) Function in_pcbbind( ) Function in_pcbconnect( ) Function in_pcbdisconnect( ) Function in_setsockaddr( ) and in_setpeeraddr( ) Functions in_pcbnotify( ) , in_rtchange( ) , and in_losing( ) Functions Summary hufs MCLAB Introduction Protocol Control Block은 UDP, TCP socket에 필요한 정보 들을 저장 Internet Protocol은 Internet protocol control block과 TCP control block을 유지 UDP는 connectionless이기 때문에 유지 X (UDP control block X) Foreign, Local IP address and port, IP header proto type, IP option, routing table entry, TCP control block state information hufs MCLAB File Type DTYPE_SOCKET Socket Type SOCK_DGRAM SOCK_STREAM Protocol layer called inpcb{}를 만들어서 socket{} 연결 TCP tcpcb{} 생성 inp_ppcb와 t_inpcb 연결 UDP null pointer Socket pair들로부터 ip addr, port 입력 Internet PCB는 double linked list Head는 gloval inpcb structure (udb , tcp) 3개의 member, lport는 후에 단명포트로.. Internet PCB는 transport layer data structure Used by TCP, UDP, raw IP Not by IP, ICMP, IGMP hufs MCLAB Code Introduction Source Global Variables Kernel’s malloc function에 의해서 PCBs 할당 Type of M_BUF , socket structure는 M_SOCKET hufs MCLAB inpcb Structure Double Linked List Head를 가리키는 pointer UDP list udp , TCP list tcp Network Byte Order Transport Layer 마다 port number 저장 X TCP의 경우 tcpcb에 속해있다 User output의 경우 kernel start, Socket layer에 대응하는 Internet PCB 연결 Processing a received IP datagram의 경우 Kernel start, PCB에 대응하는 Socket Structure와 연결 struct route inp_route Foreign address로 가는 route의 주소를 inp_route entry에 저장 hufs MCLAB inp_flags member struct ip inp_ip Copy of IP header, but only used by TOS and TTL TOS is initialized to 0 TTL is initialized by transport layer(default TTL to 64) IP_TOS or IP_TTL socket option 사용으로 변경하면 새로운 값은 inpcb.inp_ip struct에 저장 TCP, UDP는 이 structure를 사용하고, prototype IP header 처럼 IP datagram을 전송할 때 사용한다. hufs MCLAB struct mbuf *inp_options IP_OPTIONS socket option은 출발지 route를 설정할 때 사용 IP_OPTIONS socket option을 사용해서 process는 datagram을 outgoing ip_pcbopts()에 의해서 mbuf 안에 option의 copy가 저장, pointer는 mbuf를 가리킨다. struct ip_moptions *inp_moptions User’s IP multicast option에 대한 pointer hufs MCLAB in_pcballoc( ) and in_pcbdetach( ) Functions Internet PCB is allocated by TCP, UDP, raw IP when a socket is created. PRU_ATTACH request은 socket system call에 의해 나타남. 60-70 Allocate PCB and initialize to zero PCB 안에서 IP, port number가 0로 initialize 되야 하므로.. 71-74 Link Structures together insque(inp, head); 새로운 PCB inp를 double linked list hufs MCLAB 288-300 socket structure 안의 PCB pointer를 0 으로 set sofree(so) struct socket release If mbuf에 IP options이 있으면 release by m_free() If route is held by this PCB, release by rtfree() multicast options are also release by ip_freemoptions() 301-302 Double linked list에서 PCB 제거 Kernel 에 할당된 memory 를 FREE() hufs MCLAB Binding, Connecting, and Demultiplexing Binding of Local IP Address and Port Number PCB에 local port 가 존재하기 때문에 EADDRINUSE error 만약 포트를 사용한다면.. SO_REUSEADDR SO_REUSEPORT Connecting a UDP Socket TCP client 뿐만 아니라 , UDP client, server 도 가능 Socket을 특정한 peer 만 UDP datagram을 교환하도록 제한 hufs MCLAB Demultiplexing of Received IP Datagrams by TCP TCP는 in_pcblookup() 을 통해서 Internet PCB의 list에서 matching 되는 port를 찾아서 segment를 receive Match 0, 1 (local IP or foreign IP), 2 (both local IP and foreign IP) 이때 wildcard match를 한다. Wildcard match 최소값을 match Incoming segment from {140.252.1.11:1500} to {140.252.1.29:23} Incoming segment from {140.252.1.11:1501} to {140.252.1.29:23} Match ?? hufs MCLAB Demultiplexing of Received IP Datagrams by UDP 1. Broadcast, Multicast IP address로 들어오는 UDP datagram에 대 해서 모두 socket에 matching한다. 2. Unicast IP address로 들어오는 UDP datagram에 대해서는 오직 하나의 socket에 matching한다. 이때 최소값의 wildcard match를 적용 Received datagram from {140.252.13.34:1500} to {140.252.13.63:577} Received datagram from {140.252.1.11:1500} to {140.252.1.29:577} hufs MCLAB in_pcblookup( ) Function The function in_pcblookup serves four different purposes 1. TCP or UDP가 IP datagram을 받을 때, in_pcblookup()은 Internet PCBs의 protocol’s list를 scan, PCB에 matching되는 datagram을 receive. Transport layer demultiplexing of received datagram 2. Process가 bind() system call에 의해서 실행될 때, local IP addr, local port를 할당, 이때 in_pcbbind는 protocol에 의해 call하고, 요 청된 local addr pair가 이미 할당되는지를 확인 3. Process가 bind() system call에 의해서 실행될 때, 요청한 ephemeral port가 socket에 할당, kernel은 in_pcbbind에 의해 사용 가능한 port 인지 확인, 만약 사용하고 있다면 next ephemeral port 를 할당, 할당될 때 까지 4. Process가 connect() system call에 의해서 실행될 때, in_pcbbind() 는 요청된 socket pair가 unique 한지 확인 in_pcbbind() call in_pcblookup hufs MCLAB Two options confuse the logic of the function 1. SO_REUSEADDR or SO_REUSEPORT socket option 은 중복된 local address를 허용 2. Wildcard match OK는 socket이 다른 local interface에 도착하는 UDP datagram을 accept 하는 것을 의미하고, Wildcard match를 금하는 경우는 foreign IP address and port number로 connecting 했을 때를 말함 hufs MCLAB Compare local port number Compare local address Compare foreign address Compare foreign port number 만약 Wildcard를 사용하지 않으면 loop Remember best match return if exact macth found hufs MCLAB Example – Demultiplexing of Received TCP Segment laddr=140.252.1.29 , lport=23 , faddr = 140.252.1.11 , fport=1500 1. First loop 2. Second loop 3. Third loop Wildcard is 1(foreign IP) Wildcard is 2(foreign IP,local IP) Wildcard is 0 matchwild 1 matchwild 1 matchwild 0 Break; hufs MCLAB in_pcbbind( ) Function Bind a local address and port number to a socket. Explicit bind 1. From bind for a TCP socket (normally to bind a server’s well-known port) 2. From bind for UDP socket (either to bind server’s well-known port or to bind an ephemeral port to a client’s socket) 3. From connect for a TCP socket, if the socket has not yet been bound to a nonzero port (this is typical for TCP clients) 4. From listen for a TCP socket, if the socket has not yet been bound to a nonzero port (this is rare, since listen is called by a TCP server, which normally binds a well-known port, not an ephemeral port) 5. From in_pcbconnect, if the local IP address and local port number have not been set (typical for a call to connect for a UDP socket or for each call to sendto for an unconnected UDP socket) Implicit bind hufs MCLAB IP addr에 assign 되었는지 확인 Wildcard 사용 여부를 확인 nam이 NULL 이면 implicit bind Non NULL 이면 explicit bind hufs MCLAB Multicast 라면 REUSE 설정 (port) !INADDR_ANY인데 interface가 0면 error Port 존재하면 Global , all zero addr로 주소 결정 = 0 일 때 원하는 addr를 찾은 경우 사용 가능한 port 결정 hufs MCLAB t in_pcbconnect( ) Function in_pcbconnect specifies the foreign IP address and port number for a socket. 1. From connect for a TCP socket (required for a TCP client) 2. From connect for a UDP socket (optional for a UDP client, rare for a UDP server) 3. From sendto when a datagram is output on an unconnected UDP socket (common) 4. From tcp_input when a connection request (SYN segment) arrives on a TCP socket that is in the LISTEN state (standard for a TCP server) hufs MCLAB Argument 에 대한 check Global , Primary IP addr 0.0.0.0 (wildcard) 인지 broadcast 구분 hufs MCLAB Route release Route가 있으면 Route와 addr가 다르면 DONTROUTE set 이면 DONTROUTE가 없고 route 경로가 없으면 ROUTE 경로 설정 loopback이 아니면 Outgoing interface 결정 interface addr 현재 지정된 addr hufs MCLAB Broadcast 주소 사용가능한 addr 없다는 error Multicast 일 경우 Mbuf의 moptions 가져오고, multicast addr 을 addr로 저장 addr 를 route addr로 저장 socket pair가 unique 한지 check unique 하지 않으면 error Implicit bind ephemeral port hufs MCLAB in_pcbdisconnect( ) Function Remove foreign association Unconnected UDP socket에서 datagram 전송 시 Process sendto( ) in_pcbconnect( ) udp_output( ) datagram send in_pcbdisconnect( ) Connected UDP socket에서 connect가 call 했을 때 Foreign IP address to all 0s (INADDR_ANY) Foreign port number to 0 PCB를 재사용 하기 위해서 284-285 PCB가 file table을 더 이상 참조하지 않을 때 (SS_NOFDREF is set) in_pcbdetach( ) release the PCB hufs MCLAB in_setsockaddr( ) and in_setpeeraddr( ) Functions getsockname system call Local protocol address of a socket PRU_SOCKADDR request Call in_setsockaddr( ) getpeername system call Foreign protocol address PRU_PEERADDR request Call in_setpeeraddr( ) hufs MCLAB in_pcbnotify( ) , in_rtchange( ) , and in_losing( ) Functions Processing of ICMP errors Route 경로를 재설정 , All IP datagram에 영향을 주므로 따로 전달 TCP에 의해서 호출 ICMP error message struct protosw{ } 안의 pr_ctlinput entry source buffer Full hufs MCLAB notify tcp_notify or udp_notify cmd , dest address family verify Redirect handle PRC_HOSTDEAD old error, not used Global array를 errno value 로 후에 notify( )의 argument Call notify function for selected PCBs hufs MCLAB in_rtchange Function ICMP error is redirect 일때, in_pcbnotify()가 call PCB가 route를 hold 하면 rtfree, 그리고 empty 이 function에서 route update 하지 않고, 다음에 PCB가 사용될 때 new route가 ip_output에 의해 할당 ICMP Errors and UDP Sockets UDP socket이 connect가 아니면 application은 받지 못한다. Socket의 foreign IP addr, port로 제한했기 때문 in_pcbnotify() dst argument 안에 foreign IP addr, port 가 있어야 하는데, connect UDP가 아니면 inp_faddr, inp_fport를 저장 X UDP socket은 ICMP error를 못 받는다. hufs MCLAB in_losing Function 4번 이상의 retransmission timer 초과 시 TCP에 의해서 호출 Generate routing message rt_addrinfo structure는 실패한 route 정보로 체워져 있다. Delete or release route RTF_DYNAMIC redirect route는 RTM_DELETE의 요청을 하는 rtrequest()에 의해 delete hufs MCLAB Summary Internet PCB는 Internet socket(TCP, UDP, raw IP) 관련 All Internet socket에 공통된 정보를 포함 All PCBs은 protocol에 의해서 doubly linked list로 구성 Local and foreign IP addresses, route structure pointer udp , tcp … 1. in_pcblookup 는 TCP, UDP에 의해서 call, received datagram에 대해서 demultiplex, 이때 wildcard match Also in_pcbbind, in_pcbconnect call 해서 local address, process가 unique한지, 또 local,foreign address,process가 unique한지 확인 2. in_pcbbind는 explicitly, implicitly 하게 local address, port를 bind explicitly는 process가 bind call, implicitly는 TCP, UDP가 bind 없이 connect 하거나 sendto call 할 때 3. in_pcbconnect는 foreign address, port를 set local address가 set 되지 않았다면 foreign address의 경로에 적합한 local interface가 local address, local port가 set 되지 않았다면 in_pcbbind가 socket 에 ephemeral port를 선택 hufs MCLAB Summary of in_pcbbind( ) and in_pcbconnect( ) hufs MCLAB