25
Lab 3: Simple Router CS144 Lab 3 sec7on October 23 rd , 2009 Tom Wiltzius Based on slides by Derrick Isaacson, Ben Nham, Clay Collier, and Mar7n Casado

Lab 3: Simple Router - Stanford University

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab 3: Simple Router - Stanford University

Lab3:SimpleRouter

CS144Lab3sec7onOctober23rd,2009

TomWiltzius

BasedonslidesbyDerrickIsaacson,BenNham,ClayCollier,andMar7nCasado

Page 2: Lab 3: Simple Router - Stanford University

AssignmentOverview

•  YouuseavirtualnetworktopologyaspartoftheVNSsystem

•  YouwritearouterinC•  YourrouterwillrouterealIPpacketsovertheInternetfromstandardclients(i.e.ping,traceroute,Tp,Firefox…)

•  DueThursday,Oct29th•  Getstarted!

Page 3: Lab 3: Simple Router - Stanford University

Ge[ngStarted

•  Gettheassignmentcodefrom:http://www.scs.stanford.edu/09au‐cs144/lab/router.tar.gz

•  LookintheINSTRUCTIONSfileforinfoon– Usingyourassignedtopology– Whatthedefaulttopologylookslike–  Informa7onontherou7ngtableforyourtopology

Page 4: Lab 3: Simple Router - Stanford University

ProtocolsYouNeedtoHandle

•  Ethernet–  AllpacketsgiventoyouarerawEthernetframes

•  IP•  ARP–  NeededtoresolveIPaddressestoMACaddresses

•  ICMPrequests/replies–  Usedbysomeprogramstosendrequests(ping)–  Neededtosendcontrolmessagesbacktohost

•  Seesr_protocol.handNetworkSorcerytodealwiththerawbits

•  Makesureyouunderstandyourpointerarithme7c!

Page 5: Lab 3: Simple Router - Stanford University

RouterBasics

myth elaine

CampusNetwork

Your Router vns‐firewall

eth0

eth1

eth2

eth0:

192.168.128.6

eth2:

192.168.129.106

eth1:

192.168.128.50

192.168.128.51

192.168.129.107

Tp192.168.128.51

192.168.128.51 192.168.128.51

192.168.128.51

Page 6: Lab 3: Simple Router - Stanford University

Rou7ngTable

•  Sta7crou7ngtableinthisassignment•  MakesureyouunderstandLPM•  Loadedforyoufromthecommandlineintotherouter

contextoftypestructsr_instance(sr_router.h)•  Entriesarealinkedlistoftypestructsr_rt(sr_rt.h)•  Walkovertherou7ngtablelinearlytodoalongestprefix

matchonit

0.0.0.0

255.255.255.255

255.255.255.255

NetworkMask

eth0172.24.74.170.0.0.0

eth2192.168.129.106192.168.129.106

eth1192.168.128.51192.168.128.51

InterfaceNext‐hopIPaddress

Page 7: Lab 3: Simple Router - Stanford University

BasicForwardingPrinciples•  RemoveIPdatagramfromEthernetframe.

–  CouldalsobeanARPpayloadinEthernetframe•  Verifychecksum.Ifit’snotvalid,dropthepacket.•  Inspectthepacket’sDA.Assumingthepacketisdes7nedfor

someoneelse:–  Lookupnext‐hopaddressbydoingaLPMontherou7ngtableusing

thepacket’sDA•  Ifitdoesnotexist,sendICMPhostunreachable

–  DecrementTTL,updateheaderchecksum•  IfTTL==0aTerdecremen7ng,sendICMP7meexceeded

–  Fromnext‐hopIPaddress,determineoutgoinginterfaceandnext‐hopMACaddress•  Ifnecessary,sendARPrequesttodetermineMACaddress

–  EncapsulateIPdatagraminEthernetpacket–  Forwardpackettooutgoinginterface

Page 8: Lab 3: Simple Router - Stanford University

PacketsDes7nedfortheRouter

•  Ifthepacket’sDAisdes7nedtowardsoneofourinterfaces:–  Ifit’sanICMPechorequest,generateanICMPechoreply

– Otherwiseifit’saTCPorUDPpacket,generateanICMPportunreachable(neededfortraceroutetowork)

Page 9: Lab 3: Simple Router - Stanford University

Checksums

•  IPchecksum– NeedtocheckforallIPpacketheaders;droppacketifchecksumisbad

•  ICMPchecksum– Needtovalidateforincomingpacketsdes7nedforus– Needtocalculateforoutgoingpackets–  Ignoreifforwarding

•  TCP/UDPchecksum–  End‐to‐endchecksum,ignore

•  Usecksumfuncfrompreviouslabtocalculatechecksums

Page 10: Lab 3: Simple Router - Stanford University

IPAddressesandMACAddresses•  EachIPaddressisassociated

withasingleinterface•  EachinterfacehasaMAC

address•  Therefore,eachIPaddressis

associatedwithasingleMACaddress

•  Routerwith4interfaceshas4IPsand4MACaddresses!–  eth0hassomeIP0–  eth1hassomeIP1–  eth2hassomeIP2–  eth3hassomeIP3

•  Conclusion:AnIPaddressnamesaninterface,notahost

eth0

eth1

eth2

eth3

5.10.1.1

12.1.1.20012.1.1.1

12.1.0.1 IP1

IP2

IP0

IP3

Page 11: Lab 3: Simple Router - Stanford University

IPWithinEthernet

•  Inarouter,weoperateonrawEthernetframes,withanIPorARPpacketaspayload

•  Toforwardapacketonehop,mustknow:– Des7na7onIPaddress– Next‐hopMACaddressofthenext‐hopIPaddress

Byte0 Byte1 Byte2 Byte3

DestMACAddress

DestMACAddress(con7nued) SourceMACAddress

SourceMACAddress(con7nued)

Ethertype Payload

Page 12: Lab 3: Simple Router - Stanford University

NecessityofARPIPAddress Netmask Gateway Interface

0.0.0.0 0.0.0.0 5.10.1.1 eth0

12.1.0.0 255.255.0.0 12.1.0.1 eth1

12.1.1.0 255.255.255.0 12.1.1.1 eth2

12.1.1.200 255.255.255.255 12.1.1.200 eth3

•  SupposeIgetapacketdes7nedfor128.12.94.3–  Matchesdefaultroute

only(firstentry)

•  NeedtosendrawEthernetframetotheMACaddressof5.10.1.1

•  Butourrou7ngtablehasonlyIPaddresses,notMACaddresses

eth0

eth1

eth2

eth3

5.10.1.1

12.1.1.20012.1.1.1

12.1.0.1

Page 13: Lab 3: Simple Router - Stanford University

ARPPacketFormat

•  Correspondstostructsr_arphdr–  HTYPE=ar_hrd=htons(ARPHDR_ETHER)–  PTYPE=ar_pro=htons(ETHERTYPE_IP)–  HLEN=ar_hln=#bytesinMACaddress–  PLEN=ar_pln=#bytesinIPaddress–  OPER=ar_op=htons(ARP_REQUEST)orhtons(ARP_REPLY)–  SHA/SPA=senderMAC/IPaddress(copyfromsr_ifofoutgoingIF)–  THA/TPA=targetMAC/IPaddress

Page 14: Lab 3: Simple Router - Stanford University

Genera7ngARPRequest

•  Request:WhohasIP192.168.1.3?CreateARPrequestwithfields:– SourceHWaddr:MACsrc– Sourceprotocoladdr:IPsrc– TargetHWaddr:FF:FF:FF:FF:FF:FF

– Targetprotocoladdr:192.168.1.3•  ARPrequestsaresenttotheEthernetbroadcastaddress

Page 15: Lab 3: Simple Router - Stanford University

HandlingARPRequest

•  Getrequest:Whohas192.168.1.3•  IfoneoftheIPsofmyrouteris192.168.1.3,sendanARPreply:IhaveIPof192.168.1.3withMACaddressof00‐11‐22‐33‐44‐55‐66(sixbytesinhex,or48bits)–  SourceHWaddr:00‐11‐22‐33‐44‐55‐66–  Sourceprotocoladdress:192.168.1.3–  TargetHWaddr:MACsrc–  Targetprotocoladdr:IPsrc

•  ARPreplyissentdirectlytoMACsrc

Page 16: Lab 3: Simple Router - Stanford University

HandlingARPReply

•  Reply:IhaveIPof192.168.1.3withMACaddressof00‐11‐22‐33‐44‐55‐66

•  IfthetargetIPoftheARPreplyistheIPoftheinterfacethiscameinon:– AddtheIPtoMACmappingtotheARPcache

– Sendoffanypacketsthatwerewai7ngonthisARPreply

Page 17: Lab 3: Simple Router - Stanford University

Lab3ARPFunc7onality

•  GenerateARPrequestsifanexthopMACisnotintheARPcache

•  ProcessARPrepliesthattargetusandplacethemintotheARPcache

•  ProcessARPrequeststhattargetusandgenerateanappropriateARPreply

•  GothroughARPrequestqueueeverysecondandsendoffARPrequestseverysecond(editsr_arpcache_sweepreqsinsr_arpcache.c)

Page 18: Lab 3: Simple Router - Stanford University

HandlingARP

•  Rou7ngtablecontainsnext‐hopIPs,butyouneedbothanext‐hopIPandanext‐hopMACaddress

•  Whatyouhavetodo:–  GenerateARPrequestsandparseARPreplies–  ListentoARPrequestsandsendARPreplies–  Don’tsendarequestforeachpacket;instead,useanARPcache–  Requestsshould7meoutaTer5triesofabout1secondeach–  ARPcacheentriesshould7meoutaTerabout15seconds

ARPCache

……

00:e0:81:04:08:9b172.24.74.130

EthernetMACAddressIPaddress

Page 19: Lab 3: Simple Router - Stanford University

ARPCacheClass

•  We’vegivenyouanARPcacheclasscontaining–  ARPrequestqueue–  ARPcache

•  ARPcacheentries7meoutautoma7callyaTer15seconds•  Thecacheclassisessen7allytwolinkedlists:oneforthe

cache,andoneforrequests.Therearefunc7onstohandlequeryingandinser7ngintothesetwolists.

•  ARPrequestqueuewrioensothatitiseasytoconsolidateARPrequestsacrossmul7plepackets,andenforcetheARPrequest7meout

•  Seepseudocodeinsr_arpcache.hformoredetailedinfo

Page 20: Lab 3: Simple Router - Stanford University

ICMP

•  Usedtosendcontrolmessagesbacktosendinghost

•  MustprocessICMPEchoRequest•  Mustgenerate–  ICMPEchoReply–  ICMPDes7na7onHostUnreachable–  ICMPDes7na7onPortUnreachable–  ICMPTimeExceeded

•  CheckNetworkSorcerypagesondetailsofhowtoformatthesemessage

Page 21: Lab 3: Simple Router - Stanford University

BasicLab4FlowDiagramReceiveRaw

EthernetFrame

ProcessIPPacket

ProcessPacketSenttoSelf

ForwardPacket

ProcessARPRequest/Reply

IfICMPechoreq,sendICMPechoreply

IfIP+TCP/UDP,sendICMPportunreachable

DoLPMonrou7ngtable

Seepreviousslides

SendICMPhostunreachable

CheckARPcache

Sendframetonexthop

AddrequesttoARPqueue,sendingifnecessary

Nomatch

Ifalreadysent57mes

Match

Nohit

Hit

NotsenttoselfSenttoself

Page 22: Lab 3: Simple Router - Stanford University

MoreLab4Thoughts

•  Organizeyourcode–  S7ckingeverythingsr_router.cwillprobablygiveyouaheadache

– Makesomenewfiles(sugges7ons,notnecessary):•  sr_arp.c/hforhandling/genera7ngARPpackets•  sr_icmp.c/hforhandling/genera7ngICMPpackets•  sr_ip.c/hfortohandlegenera7ngIPpackets•  AddthesourcesandheaderstotheMakefile

•  Doonethingata7me–  NeedARPtosendanythingatall–  IfyoujustdotheforwardingpathwithoutICMP,shouldbeabletoroutepacketstotheappservers

–  CanaddICMPsupportlast

Page 23: Lab 3: Simple Router - Stanford University

RequiredFunc7onality

•  Forwardingpacketsshouldwork•  HandlesandgeneratesARPrequests/repliescorrectly•  YoucandownloadafileusinghopandTpfromoneofthe

appserversbehindtherouter•  Youcantraceroute(tracepath)toandthroughtherouter•  Youcanpingtoandthroughtherouter•  ICMPhostandportunreachablemessagesaregenerated

correctly•  ICMP7meexceededmessagesaregeneratedcorrectly•  Noshortcutstaken(don’tforwardapackettoevery

interface)•  Shouldnotcrash,evenwithamalformedpacket

Page 24: Lab 3: Simple Router - Stanford University

MainFunc7onsandStructures

•  Insr_router.h:–  structsr_instanceisthecontextoftherouter

•  Insr_router.c:–  sr_handlepacketiscalledforeverypacketthatgoesthroughthe

router—youhavetofillitout•  sr_protocol.hcontainsconveniencestructsforaccessing

fieldsinpackets–  Note:onlythebasicICMPheaderisprovided;sr_protocol.h

doesn’tincludestructsforallthevariousICMPpackettypesyou’llneedsoyou’llhavetomakeyourown

•  sr_if.hcontainsmethodsforge[nginforma7onabouttherouter’sinterfaces

•  ARPCacheinsr_arpcache.h

Page 25: Lab 3: Simple Router - Stanford University

Sugges7ons

•  Thinkbeforecoding—there’sagoodamountofcodetowrite

•  ReadthroughtheNetworkSorcerypagestounderstandtheprotocols

•  ReadtheFAQ•  Postandcheckthenewsgroup•  Startearly