+ All Categories
Home > Documents > Tc Pip Stack

Tc Pip Stack

Date post: 06-Jul-2018
Category:
Upload: sangeetha-sangu-bc
View: 215 times
Download: 0 times
Share this document with a friend

of 22

Transcript
  • 8/17/2019 Tc Pip Stack

    1/22

     

    Linux TCP/IP Stack 

  • 8/17/2019 Tc Pip Stack

    2/22

     

    1: Physical

    Layer 

    2: Data

    Link 

    4: Transport

    3: Netork 

    !: "pplication

    #: Presentation

    $: Session

    Inter%ace Layer &'thernet( etc)*

    Protocol Layer &TCP / IP*

    Socket layer 

    Process

      TCP / IP +s) ,SI -o.el

  • 8/17/2019 Tc Pip Stack

    3/22

     

    TCP/IP Stack ,+er+ie

      Process

    1: sosend &))) *

      Socket Layer 

    2: tcp_output & ) *

      Protocol Layer &TCP Layer*

    3: ip_output & ) *

      Inter%ace Layer &'thernet De+ice Dri+er*

    ,utput 0ueue

    5: rec+%ro-&)*

    Input 0ueue

    3: ip_input & ))) *

    4: tcp_input & ))) *

      Protocol Layer &IP Layer*

    4: ethernet_output & ) * 2: ethernet_input & )) *

    Physical e.ia

  • 8/17/2019 Tc Pip Stack

    4/22

     

    Process Layer to TCP Layer 

    sen. &int socket( const char u%( int lenth( int %las*

    Process

    5ernel sen.to &int socket( const char .ata6u%%er( int lenth( int %las( struct socka..r .estination( int .estination 6lenth*

    sen.it &struct proc p( int socket( struct -sh.r -p( int %las( int return6si7e*

    sosen. &struct socket s( struct -u% a..r( struct uio uio( struct -u% top( struct -u% control( int %las *

    uipc6syscalls)c

    uipc6socket)c

    tcp6userre8 &struct socket s( int re8uest( struct -u% -( struct -u% na-( struct -u% control * tcp6userre8)c

    tcp6output &struct tcpc tp* tcp6output)cTCP Layer 

  • 8/17/2019 Tc Pip Stack

    5/22

     

    Socket Layer 

    sen.to &int socket( const char .ata6u%%er( int lenth( int %las( struct socka..r .estination( int .estination 6lenth*

    Data

    Data

    9nuse. Space

    1$ ;ytes

    Data

    12< ;ytes

    -;u% 

     1 ;ytes

     2< ;ytes

     

    2 ;ytes

     

    $ ;ytes

    $< ;ytes

    -6nextpkt = N9LL

    -6next = N9LL-6next

    -6nextpkt = N9LL

    -6len = 1 -6len = $

    -6.ata -6.ata

    -6type = T6D"T" -6type = T6D"T"

    -6%las = 6P5T>D? -6%las =

    -6pkth.r)len = 1$

    -6pkth.r)rec+i% =N9LL

    .ata6u%%er 

    MBUF Chain

  • 8/17/2019 Tc Pip Stack

    6/22

     

    Socket Layer @sosen. passes .ata an. control in%or-ation to the protocol layersosen.&struct socket s( struct -u% a..r( struct uio uio( struct -u% .ata6u%%er( struct -u% control( int %las *

    Initiali7e a ne -e-ory u%%er an.

      +ariales to hol. %las

    yes

    Is there enouh spacein the u%%er 

    sspace&s@As6sn.*

    no

    Copy .ata6u%%er -u% 

    Bree the -e-ory u%%ers

    recei+e.1 ore u%%ers

    to sen.

     yes

      no

    int error = tcp_usrreq(s, flas, !"uf, addr, control#

     error 

    ?eturn +alue o% error to sen.to & *

  • 8/17/2019 Tc Pip Stack

    7/22

     

    TCP Layer @ tcp6usrre8&struct socket s( int re8uest( struct -u% .ata6u%%er( -u% na-( -u% control*

    Initiali7e internet protocol control lock inp an.

    TCP control lock tp

    to store in%or-ation use%ul %or TCP

      Con+ert Socket to

    Internet Protocol Control ;lock 

      inp = sotoinpc&so*

    Con+ert the internet protocol control lock 

      to a tcp control lock

    tp = intopc&inp*

    re8uest

    P?96S'ND

    int error = tcp6output&tp*return error 

    to tcp6userre8& *

  • 8/17/2019 Tc Pip Stack

    8/22

     

    Calle. y tcp6usrre8 %or one o% the %olloin reasons:

    To sen. the initial SN

    To sen. a %inishe.6sen.in -essae

    To sen. .ata

    To sen. a in.o up.ate a%ter .ata has een recei+e.)

    tcp6ouput & * %unctionality:

     1) .eter-ines hether TCP can sen. a se-ent or not .epen.in on:

      %las in the .ata sent y the socket layer to sen. an "C5( etc)

    Si7e o% in.o a.+ertise. y the recei+erEs en.)

    "-ount o% .ata rea.y to sen.hether unacknole.e. .ata alrea.y exists %or the connection

    2) Calculate the a-ount o% .ata to e sent .epen.in on:

    si7e o% recei+erEs in.o

    nu-er o% ytes in the sen. u%%er 

    3) Check %or in.o shrink 

    4) Sen. a se-ent"llocate a u%%er %or the TCP an. IP hea.er %ro- the hea.er te-plate

    Copy the TCP an. IP hea.er te-plate into the the u%%er to e sent)

    Bill the %iel.s in the TCP hea.er)

      Decre-ent the nu-er o% u%%ers to te sent( so that the en. can e checke.)

    Set se8uencenu-er an. acknole.e-ent %iel.)

    Set three %iel.s in the IP hea.er @ IP lenth( TTL an. Tos)

    Pass the .atara- to IP

    TCP Layer (tcp_output$c# @ tcp6output&struct tcpc tp*

  • 8/17/2019 Tc Pip Stack

    9/22

     

    TCP Layer (tcp_output$c# @ tcp6output&struct tcpc tp*

    struct socket so = tp @A t6inpc @A inp6socket

    Initiali7e a tcp hea.er tcp_header 

    i.le

    I.le is true i% the -ax se8uence nu-ere8uals the ol.est unacknole.e. se8uence nu-er(

    i% an "C5 is not expecte. %ro- the other en.)

    int i.le = &tp @A sn.6-ax == tp @A sn.6una*

    Check "C5 Bla

    "cknole.e-ent isnot expecte.( set the

    conestion in.o to

    one se-ent

    tp @A sn.6cn. =

    tp @A t6-axseF

    true

    %alse

  • 8/17/2019 Tc Pip Stack

    10/22

     

    TCP Layer @ tcp6output&struct tcpc tp*

    Deter-ine lenth o% .ata that shoul.

     e trans-itte. an. the %las to e use.)

    len is the -ini-u- nu-er o% ytes in thesen. u%%er(

    in &the -ini-u- o% the recei+erEs in.o*

    an. the conestion in.o)

    len = -in&so @A so6sn.)s6cc( in* @ o%% 

    "cknole.e-ent is

    not expecte.( set the

    conestion in.o to

    one se-enttp @A sn.6cn. =

    tp @A t6-axseF

    o%% is the o%%set in ytes %ro- the einnin o%

    the sen. u%%er o% the %irst .ata yte to sen.)

    o%% ytes ha+e alrea.y een sent an.

    acknole.e-enton those is aaite.)

    int o%% = tp @A sn.6nxt @ tp @A sn.6una

    Deter-ine the %las like T>6"C5( T>6BIN(

    T>6?ST( T>6SN%las = tcp 6out%las G tp @A t6state H

  • 8/17/2019 Tc Pip Stack

    11/22

     

    TCP Layer @ tcp6output&struct tcpc tp*

    tp @A t6%las

    TB6"C5N,JSen. acknole.e-ent

    Deter-ine the %las like T>6"C5( T>6BIN(

    T>6?ST( T>6SN

    %las = tcp 6out%las G tp @A t6state H

    tp @A t6%las

    TB6SN KK T>6?ST

    tp @A t6%las

    T>6BIN

    true

    %alse

    %alse

    trueSen. se8uence nu-er 

    or reset

    Binishe. sen.in

    true

    %alse

  • 8/17/2019 Tc Pip Stack

    12/22

     

    Ckeck %las to .eter-ine the type o% -essae:

    in.o proe

    retrans-ission

    nor-al .ata trans-ission

    Lenth o% .ata 44 ;ytes

    1 @ 4 @ 1#

    yes

    Create a ne -u% chain(

    copy the surplus .ata an.

     point it to the %irst -u% chain)

    %llocate an !"uf for the &C' )' header and data if possi"le$

    M'T>D? & -( 6D,NTJ"IT( T6>'"D?*

    6D,NTJ"IT in.icates that i% -e-ory is not a+ailale %or

    -u% then co-e out o% the routine an. return an error state)

    no

    Copy the .ata %ro- the socket sen. u%%er into the

    ne packet hea.er -u% 

    ip6output&-( tp@At6inpc @A inp6options( tp @A t6inpc @A inp6route(

      so @A so6options S,6D,N,T?,9T'( *

  • 8/17/2019 Tc Pip Stack

    13/22

     

    Packets

    .a-ae.

    ip_output$c

    ip_output(struct !"uf *!, struct !"uf *opt, struct route *ro, int flas, struct ip_!options *i!o#1) >ea.er initiali7ation

    2) ?oute Selection

    3) Source a..ress selection an. Bra-entation

    1$ +eader initialiation

    The +alue o% %lasO .eci.es hatEs to e .one ith the .ata IP6B,?J"?DINM : Borar. packet IP6?,9T'T,IB : ?oute .irectly to Inter%ace IP6"LL,J;?,"DC"ST : "llo roa.castin o% packet IP6?"J,9TP9T : Packet contains pre@constructe. hea.er 

    yes '??,? 

    i% &&%las == IP6B,?J"?DINM * KK 

    &%las == IP6?"J,9TP9T **

    no

    Sa+e hea.er lenth in hlen

    %or %ra-entation alorith-

    Construct an. initiali7e IP hea.er 

    set ip_v = 4( clear ip_off 

    assin uni8ue i.enti%ier to ip_id 

    length, offset, TTL, protocol, TOS etc

    are set y hiher layers)

    yes

    noI% the packet has to e %orar.e. to another host( i)e i% the

    -achine is actin as a router( then the IP hea.er %or %orar.e.

     packets shoul. not e -o.i%ie. y ip6output)

    Check i% there ere any errors hile a..in hea.ers in hiher 

    layers) ost o% the %iel.s o% the IP hea.er are pre .e%ine. y

    hiher layer protocols)

    I% the packet is not ein %orar.e. an. has to e sent to

    another host then initiali7e the IP hea.er)

  • 8/17/2019 Tc Pip Stack

    14/22

     

    2$ -oute .election

    Qeri%y Cache. ?oute %or

    .estination a..ress

    Bin. the inter%ace on hich the

     packet has to e place.) Ifp points tothe inter%aceEs ifnet  structure)

    I% &cache.6route == .estination*

    Locate route : Call rtalloc(dst_ip) to

    locate a route to the .estination) Bin.

    the inter%ace on hich the packet has

    to e place.) Ifp points to the

    inter%aceEs ifnet  structure) I%rtalloc(dst_ip) %ails to %in. a route(

    return host unreachale error)

    yes

    no

    " cache. route -ay e pro+i.e. to ip6output as an

    aru-ent) 9DP an. TCP -aintain a route cache

    associate. ith each socket)

    Check i% the cache. route is the correct .estination) I% a

    route has not een pro+i.e.( ip6output sets a te-porary

    route structure calle. iproute. 

    I% the cache. route is pro+i.e.( %in. the inter%ace on

    hich the %ra-e has to e sent)

    I% the packet is ein route.( rtalloc locates a route to

    the a..ress speci%ie. y dst ) I% rtalloc %ails( an

    '>,ST9N?'"C> error is enerate.) I% ip6%orar. calle.

    ip6output the error is con+erte. to an ICP error)

    I% the a..ress is %oun. then ifp is -a.e to point to thr

    ifnet structure %or the inter%ace) I% the next hop is not the packets %inal .estination( then dst is chane. to point to

    the next hop router)

  • 8/17/2019 Tc Pip Stack

    15/22

     

    3$ .ource address selection and Fra!entation

    Check i% +ali. source

    a..ress is speci%ie.)

    Select the IP a..ress o% the outoin

    inter%ace as the source a..ress)

    Does the packet ha+eto e %ra-ente.

    Bra-ent the packet i% itEs si7e is

    reater than the T9)

    I% there are no check6su- errors( sen.

    the .ata to if_output  %unction o% the

    selecte. inter%ace)

    no

    yes

    yes

    no

    The %inal section o% the ip6output ensures that the

    IP hea.er has a +ali. source IP a..ress) This

    coul.nEt ha+e een .one earlier ecause the route

    ha.nEt een selecte. yet) I% there is no source IP then

    the IP a..ress o% the outoin inter%ace is use. as the

    source IP)

    Larer packets &packets that excee. the T9* -ust

     e %ra-ente. e%ore they can e sent)

    In either case &%ra-ente. or not* the checksu- is

    co-pute. &in_cksum*) I% no errors are %oun.( the

    .ata is sent to if_output %unction o% the output

    inter%ace)

  • 8/17/2019 Tc Pip Stack

    16/22

     

    Inter%ace Layer &if_ethersu"r$c#

    ether_output(struct ifnet *ifp, struct !"uf *!"uf, struct soc/addr *destination, struct rtentr0 *routin_entr0#1) Qeri%ication

    2) Protocol@Speci%ic Processin

    3) Bra-e Construction4) Inter%ace 0ueuin)

    sen.err &'N'TD,JN*'thernet port

    up an. runnin

    i%p @A i%6%las

    &IB69P K IB6?9NNINM *

    no

    yes

    1$ erification

  • 8/17/2019 Tc Pip Stack

    17/22

     

    Inter%ace Layer (if_ethersu"r$c# @ ether6output&struct i%net i%p( struct -u% -u%(struct socka..r .estination( struct rtentry rt6entry*

    Bunction: Takes the .ata portion o% an 'thernet %ra-e ans encapsulates it ith a 14@yte hea.er an. places it on the inter%ace sen.68ueue)

    Phases: Qeri%ication( Protocol@Speci%ic Processin( Bra-e Construction( Inter%ace 0ueuin)

    %ru!ents

    i%p points to outoin inter%aceEs i%net structure

    -u% is the .ata to e sent

    .estination is the .estination a..ress

    rt6entry points o the routin entry

    )nitialie

    'thernet hea.er @ struct eth6hea.er eh

    sen.err &'N'TD,JN*'thernet port

    up an. runnin

    i%p @A i%6%las

    &IB69P K IB6?9NNINM *

    no

    yes

    erification

  • 8/17/2019 Tc Pip Stack

    18/22

     

    sen.err &'>,ST9N?'"C>*

    ?oute +ali.

    rt6entry = rtalloc1 &.estination( 1*

      1

     Next hop a ateay

    rt = rt @A rt6route

      1

    Destination respon.in

    to "?P re8uests

    I% not then .o not sen. -ore packets to a+oi. %loo.in)

    rt @A rt6%las

    ?TB6?'R'CT

    no

    erification

    'rotocol .pecific 'rocessin

  • 8/17/2019 Tc Pip Stack

    19/22

     

    'rotocol .pecific 'rocessinBunctionality: Bin.s 'thernet a..ress correspon.in to the IP a..ress o% the .estination)

    9se -6copy& * to keep the packet till

    an ack) Is rec+.)

    .estination @A sa6%a-ily

      "B6IN'T

    Sen. "?P roa.cast to %in. the

    ethernet a..ress correspon.in to the.estination IP a..ress

    Fra!e 'reparartion

  • 8/17/2019 Tc Pip Stack

    20/22

     

    ake sure there is roo- %or the 14 yte

    ethernet hea.er 

    6P?'P'ND & -( si7eo%&ethernet6hea.er*(

    6D,N,TJ"IT*

    Fra!e 'reparartion

    'rotocol .pecific 'rocessin

    Bor- the 'thernet hea.er %ro-

    ethernet %ra-e type(

    ethernet "C a..ress(unicast ethernet a..ress associate.

    ith the output inter%ace)

    e)) the .e%ault ateay %or a host

  • 8/17/2019 Tc Pip Stack

    21/22

     

    )nterface ueuin

    Fra!e 'reparartion

    Is the output 8ueue %ull

    no

    yes Discar. the %ra-e

    Bree the -e-ory u%% 

    sen.err & 'N,;9BS *

    Place the %ra-e on the

    inter%aceEs sen. 8ueue

    lestart & i%p *

      i%6sn.

    lestart & i%p *

  • 8/17/2019 Tc Pip Stack

    22/22

    Inter%ace Layer (if_le$c# @ lestart&struct i%net i%p*

    Bunction: De8ueues %ra-es %ro- the inter%ace output 8ueue an. arranes %or the- to e trans-itte. y the 'thernet Car.)

    le @A sc6i%)i%6%las

    IBB6?9NNINM

    struct le6so%tc le = le6so%tcl G i%p @A i%6unit H

    return error 

      1

    Copy the the %ra-e in -u% to the

    har.are u%%er 

    Set the IBB6,"CTIQ' on to in.icate that the

    .e+ice is usy trans-ittin)


Recommended