tcp_input.c 174 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Implementation of the Transmission Control Protocol(TCP).
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. * Mark Evans, <evansmp@uhura.aston.ac.uk>
  11. * Corey Minyard <wf-rch!minyard@relay.EU.net>
  12. * Florian La Roche, <flla@stud.uni-sb.de>
  13. * Charles Hedrick, <hedrick@klinzhai.rutgers.edu>
  14. * Linus Torvalds, <torvalds@cs.helsinki.fi>
  15. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  16. * Matthew Dillon, <dillon@apollo.west.oic.com>
  17. * Arnt Gulbrandsen, <agulbra@nvg.unit.no>
  18. * Jorge Cwik, <jorge@laser.satlink.net>
  19. */
  20. /*
  21. * Changes:
  22. * Pedro Roque : Fast Retransmit/Recovery.
  23. * Two receive queues.
  24. * Retransmit queue handled by TCP.
  25. * Better retransmit timer handling.
  26. * New congestion avoidance.
  27. * Header prediction.
  28. * Variable renaming.
  29. *
  30. * Eric : Fast Retransmit.
  31. * Randy Scott : MSS option defines.
  32. * Eric Schenk : Fixes to slow start algorithm.
  33. * Eric Schenk : Yet another double ACK bug.
  34. * Eric Schenk : Delayed ACK bug fixes.
  35. * Eric Schenk : Floyd style fast retrans war avoidance.
  36. * David S. Miller : Don't allow zero congestion window.
  37. * Eric Schenk : Fix retransmitter so that it sends
  38. * next packet on ack of previous packet.
  39. * Andi Kleen : Moved open_request checking here
  40. * and process RSTs for open_requests.
  41. * Andi Kleen : Better prune_queue, and other fixes.
  42. * Andrey Savochkin: Fix RTT measurements in the presence of
  43. * timestamps.
  44. * Andrey Savochkin: Check sequence numbers correctly when
  45. * removing SACKs due to in sequence incoming
  46. * data segments.
  47. * Andi Kleen: Make sure we never ack data there is not
  48. * enough room for. Also make this condition
  49. * a fatal error if it might still happen.
  50. * Andi Kleen: Add tcp_measure_rcv_mss to make
  51. * connections with MSS<min(MTU,ann. MSS)
  52. * work without delayed acks.
  53. * Andi Kleen: Process packets with PSH set in the
  54. * fast path.
  55. * J Hadi Salim: ECN support
  56. * Andrei Gurtov,
  57. * Pasi Sarolahti,
  58. * Panu Kuhlberg: Experimental audit of TCP (re)transmission
  59. * engine. Lots of bugs are found.
  60. * Pasi Sarolahti: F-RTO for dealing with spurious RTOs
  61. */
  62. #define pr_fmt(fmt) "TCP: " fmt
  63. #include <linux/mm.h>
  64. #include <linux/slab.h>
  65. #include <linux/module.h>
  66. #include <linux/sysctl.h>
  67. #include <linux/kernel.h>
  68. #include <net/dst.h>
  69. #include <net/tcp.h>
  70. #include <net/inet_common.h>
  71. #include <linux/ipsec.h>
  72. #include <asm/unaligned.h>
  73. #include <net/netdma.h>
  74. int sysctl_tcp_timestamps __read_mostly = 1;
  75. int sysctl_tcp_window_scaling __read_mostly = 1;
  76. int sysctl_tcp_sack __read_mostly = 1;
  77. int sysctl_tcp_fack __read_mostly = 1;
  78. int sysctl_tcp_reordering __read_mostly = TCP_FASTRETRANS_THRESH;
  79. EXPORT_SYMBOL(sysctl_tcp_reordering);
  80. int sysctl_tcp_ecn __read_mostly = 2;
  81. EXPORT_SYMBOL(sysctl_tcp_ecn);
  82. int sysctl_tcp_dsack __read_mostly = 1;
  83. int sysctl_tcp_app_win __read_mostly = 31;
  84. int sysctl_tcp_adv_win_scale __read_mostly = 1;
  85. EXPORT_SYMBOL(sysctl_tcp_adv_win_scale);
  86. /* rfc5961 challenge ack rate limiting */
  87. int sysctl_tcp_challenge_ack_limit = 100;
  88. int sysctl_tcp_stdurg __read_mostly;
  89. int sysctl_tcp_rfc1337 __read_mostly;
  90. int sysctl_tcp_max_orphans __read_mostly = NR_FILE;
  91. int sysctl_tcp_frto __read_mostly = 2;
  92. int sysctl_tcp_frto_response __read_mostly;
  93. int sysctl_tcp_thin_dupack __read_mostly;
  94. int sysctl_tcp_moderate_rcvbuf __read_mostly = 1;
  95. int sysctl_tcp_abc __read_mostly;
  96. int sysctl_tcp_early_retrans __read_mostly = 2;
  97. #define FLAG_DATA 0x01 /* Incoming frame contained data. */
  98. #define FLAG_WIN_UPDATE 0x02 /* Incoming ACK was a window update. */
  99. #define FLAG_DATA_ACKED 0x04 /* This ACK acknowledged new data. */
  100. #define FLAG_RETRANS_DATA_ACKED 0x08 /* "" "" some of which was retransmitted. */
  101. #define FLAG_SYN_ACKED 0x10 /* This ACK acknowledged SYN. */
  102. #define FLAG_DATA_SACKED 0x20 /* New SACK. */
  103. #define FLAG_ECE 0x40 /* ECE in this ACK */
  104. #define FLAG_SLOWPATH 0x100 /* Do not skip RFC checks for window update.*/
  105. #define FLAG_ONLY_ORIG_SACKED 0x200 /* SACKs only non-rexmit sent before RTO */
  106. #define FLAG_SND_UNA_ADVANCED 0x400 /* Snd_una was changed (!= FLAG_DATA_ACKED) */
  107. #define FLAG_DSACKING_ACK 0x800 /* SACK blocks contained D-SACK info */
  108. #define FLAG_NONHEAD_RETRANS_ACKED 0x1000 /* Non-head rexmitted data was ACKed */
  109. #define FLAG_SACK_RENEGING 0x2000 /* snd_una advanced to a sacked seq */
  110. #define FLAG_ACKED (FLAG_DATA_ACKED|FLAG_SYN_ACKED)
  111. #define FLAG_NOT_DUP (FLAG_DATA|FLAG_WIN_UPDATE|FLAG_ACKED)
  112. #define FLAG_CA_ALERT (FLAG_DATA_SACKED|FLAG_ECE)
  113. #define FLAG_FORWARD_PROGRESS (FLAG_ACKED|FLAG_DATA_SACKED)
  114. #define FLAG_ANY_PROGRESS (FLAG_FORWARD_PROGRESS|FLAG_SND_UNA_ADVANCED)
  115. #define TCP_REMNANT (TCP_FLAG_FIN|TCP_FLAG_URG|TCP_FLAG_SYN|TCP_FLAG_PSH)
  116. #define TCP_HP_BITS (~(TCP_RESERVED_BITS|TCP_FLAG_PSH))
  117. /* Adapt the MSS value used to make delayed ack decision to the
  118. * real world.
  119. */
  120. static void tcp_measure_rcv_mss(struct sock *sk, const struct sk_buff *skb)
  121. {
  122. struct inet_connection_sock *icsk = inet_csk(sk);
  123. const unsigned int lss = icsk->icsk_ack.last_seg_size;
  124. unsigned int len;
  125. icsk->icsk_ack.last_seg_size = 0;
  126. /* skb->len may jitter because of SACKs, even if peer
  127. * sends good full-sized frames.
  128. */
  129. len = skb_shinfo(skb)->gso_size ? : skb->len;
  130. if (len >= icsk->icsk_ack.rcv_mss) {
  131. icsk->icsk_ack.rcv_mss = len;
  132. } else {
  133. /* Otherwise, we make more careful check taking into account,
  134. * that SACKs block is variable.
  135. *
  136. * "len" is invariant segment length, including TCP header.
  137. */
  138. len += skb->data - skb_transport_header(skb);
  139. if (len >= TCP_MSS_DEFAULT + sizeof(struct tcphdr) ||
  140. /* If PSH is not set, packet should be
  141. * full sized, provided peer TCP is not badly broken.
  142. * This observation (if it is correct 8)) allows
  143. * to handle super-low mtu links fairly.
  144. */
  145. (len >= TCP_MIN_MSS + sizeof(struct tcphdr) &&
  146. !(tcp_flag_word(tcp_hdr(skb)) & TCP_REMNANT))) {
  147. /* Subtract also invariant (if peer is RFC compliant),
  148. * tcp header plus fixed timestamp option length.
  149. * Resulting "len" is MSS free of SACK jitter.
  150. */
  151. len -= tcp_sk(sk)->tcp_header_len;
  152. icsk->icsk_ack.last_seg_size = len;
  153. if (len == lss) {
  154. icsk->icsk_ack.rcv_mss = len;
  155. return;
  156. }
  157. }
  158. if (icsk->icsk_ack.pending & ICSK_ACK_PUSHED)
  159. icsk->icsk_ack.pending |= ICSK_ACK_PUSHED2;
  160. icsk->icsk_ack.pending |= ICSK_ACK_PUSHED;
  161. }
  162. }
  163. static void tcp_incr_quickack(struct sock *sk)
  164. {
  165. struct inet_connection_sock *icsk = inet_csk(sk);
  166. unsigned int quickacks = tcp_sk(sk)->rcv_wnd / (2 * icsk->icsk_ack.rcv_mss);
  167. if (quickacks == 0)
  168. quickacks = 2;
  169. if (quickacks > icsk->icsk_ack.quick)
  170. icsk->icsk_ack.quick = min(quickacks, TCP_MAX_QUICKACKS);
  171. }
  172. static void tcp_enter_quickack_mode(struct sock *sk)
  173. {
  174. struct inet_connection_sock *icsk = inet_csk(sk);
  175. tcp_incr_quickack(sk);
  176. icsk->icsk_ack.pingpong = 0;
  177. icsk->icsk_ack.ato = TCP_ATO_MIN;
  178. }
  179. /* Send ACKs quickly, if "quick" count is not exhausted
  180. * and the session is not interactive.
  181. */
  182. static inline bool tcp_in_quickack_mode(const struct sock *sk)
  183. {
  184. const struct inet_connection_sock *icsk = inet_csk(sk);
  185. return icsk->icsk_ack.quick && !icsk->icsk_ack.pingpong;
  186. }
  187. static inline void TCP_ECN_queue_cwr(struct tcp_sock *tp)
  188. {
  189. if (tp->ecn_flags & TCP_ECN_OK)
  190. tp->ecn_flags |= TCP_ECN_QUEUE_CWR;
  191. }
  192. static inline void TCP_ECN_accept_cwr(struct tcp_sock *tp, const struct sk_buff *skb)
  193. {
  194. if (tcp_hdr(skb)->cwr)
  195. tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
  196. }
  197. static inline void TCP_ECN_withdraw_cwr(struct tcp_sock *tp)
  198. {
  199. tp->ecn_flags &= ~TCP_ECN_DEMAND_CWR;
  200. }
  201. static inline void TCP_ECN_check_ce(struct tcp_sock *tp, const struct sk_buff *skb)
  202. {
  203. if (!(tp->ecn_flags & TCP_ECN_OK))
  204. return;
  205. switch (TCP_SKB_CB(skb)->ip_dsfield & INET_ECN_MASK) {
  206. case INET_ECN_NOT_ECT:
  207. /* Funny extension: if ECT is not set on a segment,
  208. * and we already seen ECT on a previous segment,
  209. * it is probably a retransmit.
  210. */
  211. if (tp->ecn_flags & TCP_ECN_SEEN)
  212. tcp_enter_quickack_mode((struct sock *)tp);
  213. break;
  214. case INET_ECN_CE:
  215. tp->ecn_flags |= TCP_ECN_DEMAND_CWR;
  216. /* fallinto */
  217. default:
  218. tp->ecn_flags |= TCP_ECN_SEEN;
  219. }
  220. }
  221. static inline void TCP_ECN_rcv_synack(struct tcp_sock *tp, const struct tcphdr *th)
  222. {
  223. if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || th->cwr))
  224. tp->ecn_flags &= ~TCP_ECN_OK;
  225. }
  226. static inline void TCP_ECN_rcv_syn(struct tcp_sock *tp, const struct tcphdr *th)
  227. {
  228. if ((tp->ecn_flags & TCP_ECN_OK) && (!th->ece || !th->cwr))
  229. tp->ecn_flags &= ~TCP_ECN_OK;
  230. }
  231. static bool TCP_ECN_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr *th)
  232. {
  233. if (th->ece && !th->syn && (tp->ecn_flags & TCP_ECN_OK))
  234. return true;
  235. return false;
  236. }
  237. /* Buffer size and advertised window tuning.
  238. *
  239. * 1. Tuning sk->sk_sndbuf, when connection enters established state.
  240. */
  241. static void tcp_fixup_sndbuf(struct sock *sk)
  242. {
  243. int sndmem = SKB_TRUESIZE(tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER);
  244. sndmem *= TCP_INIT_CWND;
  245. if (sk->sk_sndbuf < sndmem)
  246. sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
  247. }
  248. /* 2. Tuning advertised window (window_clamp, rcv_ssthresh)
  249. *
  250. * All tcp_full_space() is split to two parts: "network" buffer, allocated
  251. * forward and advertised in receiver window (tp->rcv_wnd) and
  252. * "application buffer", required to isolate scheduling/application
  253. * latencies from network.
  254. * window_clamp is maximal advertised window. It can be less than
  255. * tcp_full_space(), in this case tcp_full_space() - window_clamp
  256. * is reserved for "application" buffer. The less window_clamp is
  257. * the smoother our behaviour from viewpoint of network, but the lower
  258. * throughput and the higher sensitivity of the connection to losses. 8)
  259. *
  260. * rcv_ssthresh is more strict window_clamp used at "slow start"
  261. * phase to predict further behaviour of this connection.
  262. * It is used for two goals:
  263. * - to enforce header prediction at sender, even when application
  264. * requires some significant "application buffer". It is check #1.
  265. * - to prevent pruning of receive queue because of misprediction
  266. * of receiver window. Check #2.
  267. *
  268. * The scheme does not work when sender sends good segments opening
  269. * window and then starts to feed us spaghetti. But it should work
  270. * in common situations. Otherwise, we have to rely on queue collapsing.
  271. */
  272. /* Slow part of check#2. */
  273. static int __tcp_grow_window(const struct sock *sk, const struct sk_buff *skb)
  274. {
  275. struct tcp_sock *tp = tcp_sk(sk);
  276. /* Optimize this! */
  277. int truesize = tcp_win_from_space(skb->truesize) >> 1;
  278. int window = tcp_win_from_space(sysctl_tcp_rmem[2]) >> 1;
  279. while (tp->rcv_ssthresh <= window) {
  280. if (truesize <= skb->len)
  281. return 2 * inet_csk(sk)->icsk_ack.rcv_mss;
  282. truesize >>= 1;
  283. window >>= 1;
  284. }
  285. return 0;
  286. }
  287. static void tcp_grow_window(struct sock *sk, const struct sk_buff *skb)
  288. {
  289. struct tcp_sock *tp = tcp_sk(sk);
  290. /* Check #1 */
  291. if (tp->rcv_ssthresh < tp->window_clamp &&
  292. (int)tp->rcv_ssthresh < tcp_space(sk) &&
  293. !sk_under_memory_pressure(sk)) {
  294. int incr;
  295. /* Check #2. Increase window, if skb with such overhead
  296. * will fit to rcvbuf in future.
  297. */
  298. if (tcp_win_from_space(skb->truesize) <= skb->len)
  299. incr = 2 * tp->advmss;
  300. else
  301. incr = __tcp_grow_window(sk, skb);
  302. if (incr) {
  303. incr = max_t(int, incr, 2 * skb->len);
  304. tp->rcv_ssthresh = min(tp->rcv_ssthresh + incr,
  305. tp->window_clamp);
  306. inet_csk(sk)->icsk_ack.quick |= 1;
  307. }
  308. }
  309. }
  310. /* 3. Tuning rcvbuf, when connection enters established state. */
  311. static void tcp_fixup_rcvbuf(struct sock *sk)
  312. {
  313. u32 mss = tcp_sk(sk)->advmss;
  314. u32 icwnd = TCP_DEFAULT_INIT_RCVWND;
  315. int rcvmem;
  316. /* Limit to 10 segments if mss <= 1460,
  317. * or 14600/mss segments, with a minimum of two segments.
  318. */
  319. if (mss > 1460)
  320. icwnd = max_t(u32, (1460 * TCP_DEFAULT_INIT_RCVWND) / mss, 2);
  321. rcvmem = SKB_TRUESIZE(mss + MAX_TCP_HEADER);
  322. while (tcp_win_from_space(rcvmem) < mss)
  323. rcvmem += 128;
  324. rcvmem *= icwnd;
  325. if (sk->sk_rcvbuf < rcvmem)
  326. sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
  327. }
  328. /* 4. Try to fixup all. It is made immediately after connection enters
  329. * established state.
  330. */
  331. static void tcp_init_buffer_space(struct sock *sk)
  332. {
  333. struct tcp_sock *tp = tcp_sk(sk);
  334. int maxwin;
  335. if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
  336. tcp_fixup_rcvbuf(sk);
  337. if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
  338. tcp_fixup_sndbuf(sk);
  339. tp->rcvq_space.space = tp->rcv_wnd;
  340. maxwin = tcp_full_space(sk);
  341. if (tp->window_clamp >= maxwin) {
  342. tp->window_clamp = maxwin;
  343. if (sysctl_tcp_app_win && maxwin > 4 * tp->advmss)
  344. tp->window_clamp = max(maxwin -
  345. (maxwin >> sysctl_tcp_app_win),
  346. 4 * tp->advmss);
  347. }
  348. /* Force reservation of one segment. */
  349. if (sysctl_tcp_app_win &&
  350. tp->window_clamp > 2 * tp->advmss &&
  351. tp->window_clamp + tp->advmss > maxwin)
  352. tp->window_clamp = max(2 * tp->advmss, maxwin - tp->advmss);
  353. tp->rcv_ssthresh = min(tp->rcv_ssthresh, tp->window_clamp);
  354. tp->snd_cwnd_stamp = tcp_time_stamp;
  355. }
  356. /* 5. Recalculate window clamp after socket hit its memory bounds. */
  357. static void tcp_clamp_window(struct sock *sk)
  358. {
  359. struct tcp_sock *tp = tcp_sk(sk);
  360. struct inet_connection_sock *icsk = inet_csk(sk);
  361. icsk->icsk_ack.quick = 0;
  362. if (sk->sk_rcvbuf < sysctl_tcp_rmem[2] &&
  363. !(sk->sk_userlocks & SOCK_RCVBUF_LOCK) &&
  364. !sk_under_memory_pressure(sk) &&
  365. sk_memory_allocated(sk) < sk_prot_mem_limits(sk, 0)) {
  366. sk->sk_rcvbuf = min(atomic_read(&sk->sk_rmem_alloc),
  367. sysctl_tcp_rmem[2]);
  368. }
  369. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf)
  370. tp->rcv_ssthresh = min(tp->window_clamp, 2U * tp->advmss);
  371. }
  372. /* Initialize RCV_MSS value.
  373. * RCV_MSS is an our guess about MSS used by the peer.
  374. * We haven't any direct information about the MSS.
  375. * It's better to underestimate the RCV_MSS rather than overestimate.
  376. * Overestimations make us ACKing less frequently than needed.
  377. * Underestimations are more easy to detect and fix by tcp_measure_rcv_mss().
  378. */
  379. void tcp_initialize_rcv_mss(struct sock *sk)
  380. {
  381. const struct tcp_sock *tp = tcp_sk(sk);
  382. unsigned int hint = min_t(unsigned int, tp->advmss, tp->mss_cache);
  383. hint = min(hint, tp->rcv_wnd / 2);
  384. hint = min(hint, TCP_MSS_DEFAULT);
  385. hint = max(hint, TCP_MIN_MSS);
  386. inet_csk(sk)->icsk_ack.rcv_mss = hint;
  387. }
  388. EXPORT_SYMBOL(tcp_initialize_rcv_mss);
  389. /* Receiver "autotuning" code.
  390. *
  391. * The algorithm for RTT estimation w/o timestamps is based on
  392. * Dynamic Right-Sizing (DRS) by Wu Feng and Mike Fisk of LANL.
  393. * <http://public.lanl.gov/radiant/pubs.html#DRS>
  394. *
  395. * More detail on this code can be found at
  396. * <http://staff.psc.edu/jheffner/>,
  397. * though this reference is out of date. A new paper
  398. * is pending.
  399. */
  400. static void tcp_rcv_rtt_update(struct tcp_sock *tp, u32 sample, int win_dep)
  401. {
  402. u32 new_sample = tp->rcv_rtt_est.rtt;
  403. long m = sample;
  404. if (m == 0)
  405. m = 1;
  406. if (new_sample != 0) {
  407. /* If we sample in larger samples in the non-timestamp
  408. * case, we could grossly overestimate the RTT especially
  409. * with chatty applications or bulk transfer apps which
  410. * are stalled on filesystem I/O.
  411. *
  412. * Also, since we are only going for a minimum in the
  413. * non-timestamp case, we do not smooth things out
  414. * else with timestamps disabled convergence takes too
  415. * long.
  416. */
  417. if (!win_dep) {
  418. m -= (new_sample >> 3);
  419. new_sample += m;
  420. } else {
  421. m <<= 3;
  422. if (m < new_sample)
  423. new_sample = m;
  424. }
  425. } else {
  426. /* No previous measure. */
  427. new_sample = m << 3;
  428. }
  429. if (tp->rcv_rtt_est.rtt != new_sample)
  430. tp->rcv_rtt_est.rtt = new_sample;
  431. }
  432. static inline void tcp_rcv_rtt_measure(struct tcp_sock *tp)
  433. {
  434. if (tp->rcv_rtt_est.time == 0)
  435. goto new_measure;
  436. if (before(tp->rcv_nxt, tp->rcv_rtt_est.seq))
  437. return;
  438. tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rcv_rtt_est.time, 1);
  439. new_measure:
  440. tp->rcv_rtt_est.seq = tp->rcv_nxt + tp->rcv_wnd;
  441. tp->rcv_rtt_est.time = tcp_time_stamp;
  442. }
  443. static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
  444. const struct sk_buff *skb)
  445. {
  446. struct tcp_sock *tp = tcp_sk(sk);
  447. if (tp->rx_opt.rcv_tsecr &&
  448. (TCP_SKB_CB(skb)->end_seq -
  449. TCP_SKB_CB(skb)->seq >= inet_csk(sk)->icsk_ack.rcv_mss))
  450. tcp_rcv_rtt_update(tp, tcp_time_stamp - tp->rx_opt.rcv_tsecr, 0);
  451. }
  452. /*
  453. * This function should be called every time data is copied to user space.
  454. * It calculates the appropriate TCP receive buffer space.
  455. */
  456. void tcp_rcv_space_adjust(struct sock *sk)
  457. {
  458. struct tcp_sock *tp = tcp_sk(sk);
  459. int time;
  460. int space;
  461. if (tp->rcvq_space.time == 0)
  462. goto new_measure;
  463. time = tcp_time_stamp - tp->rcvq_space.time;
  464. if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
  465. return;
  466. space = 2 * (tp->copied_seq - tp->rcvq_space.seq);
  467. space = max(tp->rcvq_space.space, space);
  468. if (tp->rcvq_space.space != space) {
  469. int rcvmem;
  470. tp->rcvq_space.space = space;
  471. if (sysctl_tcp_moderate_rcvbuf &&
  472. !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
  473. int new_clamp = space;
  474. /* Receive space grows, normalize in order to
  475. * take into account packet headers and sk_buff
  476. * structure overhead.
  477. */
  478. space /= tp->advmss;
  479. if (!space)
  480. space = 1;
  481. rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
  482. while (tcp_win_from_space(rcvmem) < tp->advmss)
  483. rcvmem += 128;
  484. space *= rcvmem;
  485. space = min(space, sysctl_tcp_rmem[2]);
  486. if (space > sk->sk_rcvbuf) {
  487. sk->sk_rcvbuf = space;
  488. /* Make the window clamp follow along. */
  489. tp->window_clamp = new_clamp;
  490. }
  491. }
  492. }
  493. new_measure:
  494. tp->rcvq_space.seq = tp->copied_seq;
  495. tp->rcvq_space.time = tcp_time_stamp;
  496. }
  497. /* There is something which you must keep in mind when you analyze the
  498. * behavior of the tp->ato delayed ack timeout interval. When a
  499. * connection starts up, we want to ack as quickly as possible. The
  500. * problem is that "good" TCP's do slow start at the beginning of data
  501. * transmission. The means that until we send the first few ACK's the
  502. * sender will sit on his end and only queue most of his data, because
  503. * he can only send snd_cwnd unacked packets at any given time. For
  504. * each ACK we send, he increments snd_cwnd and transmits more of his
  505. * queue. -DaveM
  506. */
  507. static void tcp_event_data_recv(struct sock *sk, struct sk_buff *skb)
  508. {
  509. struct tcp_sock *tp = tcp_sk(sk);
  510. struct inet_connection_sock *icsk = inet_csk(sk);
  511. u32 now;
  512. inet_csk_schedule_ack(sk);
  513. tcp_measure_rcv_mss(sk, skb);
  514. tcp_rcv_rtt_measure(tp);
  515. now = tcp_time_stamp;
  516. if (!icsk->icsk_ack.ato) {
  517. /* The _first_ data packet received, initialize
  518. * delayed ACK engine.
  519. */
  520. tcp_incr_quickack(sk);
  521. icsk->icsk_ack.ato = TCP_ATO_MIN;
  522. } else {
  523. int m = now - icsk->icsk_ack.lrcvtime;
  524. if (m <= TCP_ATO_MIN / 2) {
  525. /* The fastest case is the first. */
  526. icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + TCP_ATO_MIN / 2;
  527. } else if (m < icsk->icsk_ack.ato) {
  528. icsk->icsk_ack.ato = (icsk->icsk_ack.ato >> 1) + m;
  529. if (icsk->icsk_ack.ato > icsk->icsk_rto)
  530. icsk->icsk_ack.ato = icsk->icsk_rto;
  531. } else if (m > icsk->icsk_rto) {
  532. /* Too long gap. Apparently sender failed to
  533. * restart window, so that we send ACKs quickly.
  534. */
  535. tcp_incr_quickack(sk);
  536. sk_mem_reclaim(sk);
  537. }
  538. }
  539. icsk->icsk_ack.lrcvtime = now;
  540. TCP_ECN_check_ce(tp, skb);
  541. if (skb->len >= 128)
  542. tcp_grow_window(sk, skb);
  543. }
  544. /* Called to compute a smoothed rtt estimate. The data fed to this
  545. * routine either comes from timestamps, or from segments that were
  546. * known _not_ to have been retransmitted [see Karn/Partridge
  547. * Proceedings SIGCOMM 87]. The algorithm is from the SIGCOMM 88
  548. * piece by Van Jacobson.
  549. * NOTE: the next three routines used to be one big routine.
  550. * To save cycles in the RFC 1323 implementation it was better to break
  551. * it up into three procedures. -- erics
  552. */
  553. static void tcp_rtt_estimator(struct sock *sk, const __u32 mrtt)
  554. {
  555. struct tcp_sock *tp = tcp_sk(sk);
  556. long m = mrtt; /* RTT */
  557. /* The following amusing code comes from Jacobson's
  558. * article in SIGCOMM '88. Note that rtt and mdev
  559. * are scaled versions of rtt and mean deviation.
  560. * This is designed to be as fast as possible
  561. * m stands for "measurement".
  562. *
  563. * On a 1990 paper the rto value is changed to:
  564. * RTO = rtt + 4 * mdev
  565. *
  566. * Funny. This algorithm seems to be very broken.
  567. * These formulae increase RTO, when it should be decreased, increase
  568. * too slowly, when it should be increased quickly, decrease too quickly
  569. * etc. I guess in BSD RTO takes ONE value, so that it is absolutely
  570. * does not matter how to _calculate_ it. Seems, it was trap
  571. * that VJ failed to avoid. 8)
  572. */
  573. if (m == 0)
  574. m = 1;
  575. if (tp->srtt != 0) {
  576. m -= (tp->srtt >> 3); /* m is now error in rtt est */
  577. tp->srtt += m; /* rtt = 7/8 rtt + 1/8 new */
  578. if (m < 0) {
  579. m = -m; /* m is now abs(error) */
  580. m -= (tp->mdev >> 2); /* similar update on mdev */
  581. /* This is similar to one of Eifel findings.
  582. * Eifel blocks mdev updates when rtt decreases.
  583. * This solution is a bit different: we use finer gain
  584. * for mdev in this case (alpha*beta).
  585. * Like Eifel it also prevents growth of rto,
  586. * but also it limits too fast rto decreases,
  587. * happening in pure Eifel.
  588. */
  589. if (m > 0)
  590. m >>= 3;
  591. } else {
  592. m -= (tp->mdev >> 2); /* similar update on mdev */
  593. }
  594. tp->mdev += m; /* mdev = 3/4 mdev + 1/4 new */
  595. if (tp->mdev > tp->mdev_max) {
  596. tp->mdev_max = tp->mdev;
  597. if (tp->mdev_max > tp->rttvar)
  598. tp->rttvar = tp->mdev_max;
  599. }
  600. if (after(tp->snd_una, tp->rtt_seq)) {
  601. if (tp->mdev_max < tp->rttvar)
  602. tp->rttvar -= (tp->rttvar - tp->mdev_max) >> 2;
  603. tp->rtt_seq = tp->snd_nxt;
  604. tp->mdev_max = tcp_rto_min(sk);
  605. }
  606. } else {
  607. /* no previous measure. */
  608. tp->srtt = m << 3; /* take the measured time to be rtt */
  609. tp->mdev = m << 1; /* make sure rto = 3*rtt */
  610. tp->mdev_max = tp->rttvar = max(tp->mdev, tcp_rto_min(sk));
  611. tp->rtt_seq = tp->snd_nxt;
  612. }
  613. }
  614. /* Calculate rto without backoff. This is the second half of Van Jacobson's
  615. * routine referred to above.
  616. */
  617. void tcp_set_rto(struct sock *sk)
  618. {
  619. const struct tcp_sock *tp = tcp_sk(sk);
  620. /* Old crap is replaced with new one. 8)
  621. *
  622. * More seriously:
  623. * 1. If rtt variance happened to be less 50msec, it is hallucination.
  624. * It cannot be less due to utterly erratic ACK generation made
  625. * at least by solaris and freebsd. "Erratic ACKs" has _nothing_
  626. * to do with delayed acks, because at cwnd>2 true delack timeout
  627. * is invisible. Actually, Linux-2.4 also generates erratic
  628. * ACKs in some circumstances.
  629. */
  630. inet_csk(sk)->icsk_rto = __tcp_set_rto(tp);
  631. /* 2. Fixups made earlier cannot be right.
  632. * If we do not estimate RTO correctly without them,
  633. * all the algo is pure shit and should be replaced
  634. * with correct one. It is exactly, which we pretend to do.
  635. */
  636. /* NOTE: clamping at TCP_RTO_MIN is not required, current algo
  637. * guarantees that rto is higher.
  638. */
  639. tcp_bound_rto(sk);
  640. }
  641. __u32 tcp_init_cwnd(const struct tcp_sock *tp, const struct dst_entry *dst)
  642. {
  643. __u32 cwnd = (dst ? dst_metric(dst, RTAX_INITCWND) : 0);
  644. if (!cwnd)
  645. cwnd = TCP_INIT_CWND;
  646. return min_t(__u32, cwnd, tp->snd_cwnd_clamp);
  647. }
  648. /* Set slow start threshold and cwnd not falling to slow start */
  649. void tcp_enter_cwr(struct sock *sk, const int set_ssthresh)
  650. {
  651. struct tcp_sock *tp = tcp_sk(sk);
  652. const struct inet_connection_sock *icsk = inet_csk(sk);
  653. tp->prior_ssthresh = 0;
  654. tp->bytes_acked = 0;
  655. if (icsk->icsk_ca_state < TCP_CA_CWR) {
  656. tp->undo_marker = 0;
  657. if (set_ssthresh)
  658. tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
  659. tp->snd_cwnd = min(tp->snd_cwnd,
  660. tcp_packets_in_flight(tp) + 1U);
  661. tp->snd_cwnd_cnt = 0;
  662. tp->high_seq = tp->snd_nxt;
  663. tp->snd_cwnd_stamp = tcp_time_stamp;
  664. TCP_ECN_queue_cwr(tp);
  665. tcp_set_ca_state(sk, TCP_CA_CWR);
  666. }
  667. }
  668. /*
  669. * Packet counting of FACK is based on in-order assumptions, therefore TCP
  670. * disables it when reordering is detected
  671. */
  672. void tcp_disable_fack(struct tcp_sock *tp)
  673. {
  674. /* RFC3517 uses different metric in lost marker => reset on change */
  675. if (tcp_is_fack(tp))
  676. tp->lost_skb_hint = NULL;
  677. tp->rx_opt.sack_ok &= ~TCP_FACK_ENABLED;
  678. }
  679. /* Take a notice that peer is sending D-SACKs */
  680. static void tcp_dsack_seen(struct tcp_sock *tp)
  681. {
  682. tp->rx_opt.sack_ok |= TCP_DSACK_SEEN;
  683. }
  684. static void tcp_update_reordering(struct sock *sk, const int metric,
  685. const int ts)
  686. {
  687. struct tcp_sock *tp = tcp_sk(sk);
  688. if (metric > tp->reordering) {
  689. int mib_idx;
  690. tp->reordering = min(TCP_MAX_REORDERING, metric);
  691. /* This exciting event is worth to be remembered. 8) */
  692. if (ts)
  693. mib_idx = LINUX_MIB_TCPTSREORDER;
  694. else if (tcp_is_reno(tp))
  695. mib_idx = LINUX_MIB_TCPRENOREORDER;
  696. else if (tcp_is_fack(tp))
  697. mib_idx = LINUX_MIB_TCPFACKREORDER;
  698. else
  699. mib_idx = LINUX_MIB_TCPSACKREORDER;
  700. NET_INC_STATS_BH(sock_net(sk), mib_idx);
  701. #if FASTRETRANS_DEBUG > 1
  702. pr_debug("Disorder%d %d %u f%u s%u rr%d\n",
  703. tp->rx_opt.sack_ok, inet_csk(sk)->icsk_ca_state,
  704. tp->reordering,
  705. tp->fackets_out,
  706. tp->sacked_out,
  707. tp->undo_marker ? tp->undo_retrans : 0);
  708. #endif
  709. tcp_disable_fack(tp);
  710. }
  711. if (metric > 0)
  712. tcp_disable_early_retrans(tp);
  713. }
  714. /* This must be called before lost_out is incremented */
  715. static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
  716. {
  717. if ((tp->retransmit_skb_hint == NULL) ||
  718. before(TCP_SKB_CB(skb)->seq,
  719. TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
  720. tp->retransmit_skb_hint = skb;
  721. if (!tp->lost_out ||
  722. after(TCP_SKB_CB(skb)->end_seq, tp->retransmit_high))
  723. tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
  724. }
  725. static void tcp_skb_mark_lost(struct tcp_sock *tp, struct sk_buff *skb)
  726. {
  727. if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
  728. tcp_verify_retransmit_hint(tp, skb);
  729. tp->lost_out += tcp_skb_pcount(skb);
  730. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  731. }
  732. }
  733. static void tcp_skb_mark_lost_uncond_verify(struct tcp_sock *tp,
  734. struct sk_buff *skb)
  735. {
  736. tcp_verify_retransmit_hint(tp, skb);
  737. if (!(TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_ACKED))) {
  738. tp->lost_out += tcp_skb_pcount(skb);
  739. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  740. }
  741. }
  742. /* This procedure tags the retransmission queue when SACKs arrive.
  743. *
  744. * We have three tag bits: SACKED(S), RETRANS(R) and LOST(L).
  745. * Packets in queue with these bits set are counted in variables
  746. * sacked_out, retrans_out and lost_out, correspondingly.
  747. *
  748. * Valid combinations are:
  749. * Tag InFlight Description
  750. * 0 1 - orig segment is in flight.
  751. * S 0 - nothing flies, orig reached receiver.
  752. * L 0 - nothing flies, orig lost by net.
  753. * R 2 - both orig and retransmit are in flight.
  754. * L|R 1 - orig is lost, retransmit is in flight.
  755. * S|R 1 - orig reached receiver, retrans is still in flight.
  756. * (L|S|R is logically valid, it could occur when L|R is sacked,
  757. * but it is equivalent to plain S and code short-curcuits it to S.
  758. * L|S is logically invalid, it would mean -1 packet in flight 8))
  759. *
  760. * These 6 states form finite state machine, controlled by the following events:
  761. * 1. New ACK (+SACK) arrives. (tcp_sacktag_write_queue())
  762. * 2. Retransmission. (tcp_retransmit_skb(), tcp_xmit_retransmit_queue())
  763. * 3. Loss detection event of two flavors:
  764. * A. Scoreboard estimator decided the packet is lost.
  765. * A'. Reno "three dupacks" marks head of queue lost.
  766. * A''. Its FACK modification, head until snd.fack is lost.
  767. * B. SACK arrives sacking SND.NXT at the moment, when the
  768. * segment was retransmitted.
  769. * 4. D-SACK added new rule: D-SACK changes any tag to S.
  770. *
  771. * It is pleasant to note, that state diagram turns out to be commutative,
  772. * so that we are allowed not to be bothered by order of our actions,
  773. * when multiple events arrive simultaneously. (see the function below).
  774. *
  775. * Reordering detection.
  776. * --------------------
  777. * Reordering metric is maximal distance, which a packet can be displaced
  778. * in packet stream. With SACKs we can estimate it:
  779. *
  780. * 1. SACK fills old hole and the corresponding segment was not
  781. * ever retransmitted -> reordering. Alas, we cannot use it
  782. * when segment was retransmitted.
  783. * 2. The last flaw is solved with D-SACK. D-SACK arrives
  784. * for retransmitted and already SACKed segment -> reordering..
  785. * Both of these heuristics are not used in Loss state, when we cannot
  786. * account for retransmits accurately.
  787. *
  788. * SACK block validation.
  789. * ----------------------
  790. *
  791. * SACK block range validation checks that the received SACK block fits to
  792. * the expected sequence limits, i.e., it is between SND.UNA and SND.NXT.
  793. * Note that SND.UNA is not included to the range though being valid because
  794. * it means that the receiver is rather inconsistent with itself reporting
  795. * SACK reneging when it should advance SND.UNA. Such SACK block this is
  796. * perfectly valid, however, in light of RFC2018 which explicitly states
  797. * that "SACK block MUST reflect the newest segment. Even if the newest
  798. * segment is going to be discarded ...", not that it looks very clever
  799. * in case of head skb. Due to potentional receiver driven attacks, we
  800. * choose to avoid immediate execution of a walk in write queue due to
  801. * reneging and defer head skb's loss recovery to standard loss recovery
  802. * procedure that will eventually trigger (nothing forbids us doing this).
  803. *
  804. * Implements also blockage to start_seq wrap-around. Problem lies in the
  805. * fact that though start_seq (s) is before end_seq (i.e., not reversed),
  806. * there's no guarantee that it will be before snd_nxt (n). The problem
  807. * happens when start_seq resides between end_seq wrap (e_w) and snd_nxt
  808. * wrap (s_w):
  809. *
  810. * <- outs wnd -> <- wrapzone ->
  811. * u e n u_w e_w s n_w
  812. * | | | | | | |
  813. * |<------------+------+----- TCP seqno space --------------+---------->|
  814. * ...-- <2^31 ->| |<--------...
  815. * ...---- >2^31 ------>| |<--------...
  816. *
  817. * Current code wouldn't be vulnerable but it's better still to discard such
  818. * crazy SACK blocks. Doing this check for start_seq alone closes somewhat
  819. * similar case (end_seq after snd_nxt wrap) as earlier reversed check in
  820. * snd_nxt wrap -> snd_una region will then become "well defined", i.e.,
  821. * equal to the ideal case (infinite seqno space without wrap caused issues).
  822. *
  823. * With D-SACK the lower bound is extended to cover sequence space below
  824. * SND.UNA down to undo_marker, which is the last point of interest. Yet
  825. * again, D-SACK block must not to go across snd_una (for the same reason as
  826. * for the normal SACK blocks, explained above). But there all simplicity
  827. * ends, TCP might receive valid D-SACKs below that. As long as they reside
  828. * fully below undo_marker they do not affect behavior in anyway and can
  829. * therefore be safely ignored. In rare cases (which are more or less
  830. * theoretical ones), the D-SACK will nicely cross that boundary due to skb
  831. * fragmentation and packet reordering past skb's retransmission. To consider
  832. * them correctly, the acceptable range must be extended even more though
  833. * the exact amount is rather hard to quantify. However, tp->max_window can
  834. * be used as an exaggerated estimate.
  835. */
  836. static bool tcp_is_sackblock_valid(struct tcp_sock *tp, bool is_dsack,
  837. u32 start_seq, u32 end_seq)
  838. {
  839. /* Too far in future, or reversed (interpretation is ambiguous) */
  840. if (after(end_seq, tp->snd_nxt) || !before(start_seq, end_seq))
  841. return false;
  842. /* Nasty start_seq wrap-around check (see comments above) */
  843. if (!before(start_seq, tp->snd_nxt))
  844. return false;
  845. /* In outstanding window? ...This is valid exit for D-SACKs too.
  846. * start_seq == snd_una is non-sensical (see comments above)
  847. */
  848. if (after(start_seq, tp->snd_una))
  849. return true;
  850. if (!is_dsack || !tp->undo_marker)
  851. return false;
  852. /* ...Then it's D-SACK, and must reside below snd_una completely */
  853. if (after(end_seq, tp->snd_una))
  854. return false;
  855. if (!before(start_seq, tp->undo_marker))
  856. return true;
  857. /* Too old */
  858. if (!after(end_seq, tp->undo_marker))
  859. return false;
  860. /* Undo_marker boundary crossing (overestimates a lot). Known already:
  861. * start_seq < undo_marker and end_seq >= undo_marker.
  862. */
  863. return !before(start_seq, end_seq - tp->max_window);
  864. }
  865. /* Check for lost retransmit. This superb idea is borrowed from "ratehalving".
  866. * Event "B". Later note: FACK people cheated me again 8), we have to account
  867. * for reordering! Ugly, but should help.
  868. *
  869. * Search retransmitted skbs from write_queue that were sent when snd_nxt was
  870. * less than what is now known to be received by the other end (derived from
  871. * highest SACK block). Also calculate the lowest snd_nxt among the remaining
  872. * retransmitted skbs to avoid some costly processing per ACKs.
  873. */
  874. static void tcp_mark_lost_retrans(struct sock *sk)
  875. {
  876. const struct inet_connection_sock *icsk = inet_csk(sk);
  877. struct tcp_sock *tp = tcp_sk(sk);
  878. struct sk_buff *skb;
  879. int cnt = 0;
  880. u32 new_low_seq = tp->snd_nxt;
  881. u32 received_upto = tcp_highest_sack_seq(tp);
  882. if (!tcp_is_fack(tp) || !tp->retrans_out ||
  883. !after(received_upto, tp->lost_retrans_low) ||
  884. icsk->icsk_ca_state != TCP_CA_Recovery)
  885. return;
  886. tcp_for_write_queue(skb, sk) {
  887. u32 ack_seq = TCP_SKB_CB(skb)->ack_seq;
  888. if (skb == tcp_send_head(sk))
  889. break;
  890. if (cnt == tp->retrans_out)
  891. break;
  892. if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
  893. continue;
  894. if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS))
  895. continue;
  896. /* TODO: We would like to get rid of tcp_is_fack(tp) only
  897. * constraint here (see above) but figuring out that at
  898. * least tp->reordering SACK blocks reside between ack_seq
  899. * and received_upto is not easy task to do cheaply with
  900. * the available datastructures.
  901. *
  902. * Whether FACK should check here for tp->reordering segs
  903. * in-between one could argue for either way (it would be
  904. * rather simple to implement as we could count fack_count
  905. * during the walk and do tp->fackets_out - fack_count).
  906. */
  907. if (after(received_upto, ack_seq)) {
  908. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  909. tp->retrans_out -= tcp_skb_pcount(skb);
  910. tcp_skb_mark_lost_uncond_verify(tp, skb);
  911. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSTRETRANSMIT);
  912. } else {
  913. if (before(ack_seq, new_low_seq))
  914. new_low_seq = ack_seq;
  915. cnt += tcp_skb_pcount(skb);
  916. }
  917. }
  918. if (tp->retrans_out)
  919. tp->lost_retrans_low = new_low_seq;
  920. }
  921. static bool tcp_check_dsack(struct sock *sk, const struct sk_buff *ack_skb,
  922. struct tcp_sack_block_wire *sp, int num_sacks,
  923. u32 prior_snd_una)
  924. {
  925. struct tcp_sock *tp = tcp_sk(sk);
  926. u32 start_seq_0 = get_unaligned_be32(&sp[0].start_seq);
  927. u32 end_seq_0 = get_unaligned_be32(&sp[0].end_seq);
  928. bool dup_sack = false;
  929. if (before(start_seq_0, TCP_SKB_CB(ack_skb)->ack_seq)) {
  930. dup_sack = true;
  931. tcp_dsack_seen(tp);
  932. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKRECV);
  933. } else if (num_sacks > 1) {
  934. u32 end_seq_1 = get_unaligned_be32(&sp[1].end_seq);
  935. u32 start_seq_1 = get_unaligned_be32(&sp[1].start_seq);
  936. if (!after(end_seq_0, end_seq_1) &&
  937. !before(start_seq_0, start_seq_1)) {
  938. dup_sack = true;
  939. tcp_dsack_seen(tp);
  940. NET_INC_STATS_BH(sock_net(sk),
  941. LINUX_MIB_TCPDSACKOFORECV);
  942. }
  943. }
  944. /* D-SACK for already forgotten data... Do dumb counting. */
  945. if (dup_sack && tp->undo_marker && tp->undo_retrans &&
  946. !after(end_seq_0, prior_snd_una) &&
  947. after(end_seq_0, tp->undo_marker))
  948. tp->undo_retrans--;
  949. return dup_sack;
  950. }
  951. struct tcp_sacktag_state {
  952. int reord;
  953. int fack_count;
  954. int flag;
  955. };
  956. /* Check if skb is fully within the SACK block. In presence of GSO skbs,
  957. * the incoming SACK may not exactly match but we can find smaller MSS
  958. * aligned portion of it that matches. Therefore we might need to fragment
  959. * which may fail and creates some hassle (caller must handle error case
  960. * returns).
  961. *
  962. * FIXME: this could be merged to shift decision code
  963. */
  964. static int tcp_match_skb_to_sack(struct sock *sk, struct sk_buff *skb,
  965. u32 start_seq, u32 end_seq)
  966. {
  967. int err;
  968. bool in_sack;
  969. unsigned int pkt_len;
  970. unsigned int mss;
  971. in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
  972. !before(end_seq, TCP_SKB_CB(skb)->end_seq);
  973. if (tcp_skb_pcount(skb) > 1 && !in_sack &&
  974. after(TCP_SKB_CB(skb)->end_seq, start_seq)) {
  975. mss = tcp_skb_mss(skb);
  976. in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
  977. if (!in_sack) {
  978. pkt_len = start_seq - TCP_SKB_CB(skb)->seq;
  979. if (pkt_len < mss)
  980. pkt_len = mss;
  981. } else {
  982. pkt_len = end_seq - TCP_SKB_CB(skb)->seq;
  983. if (pkt_len < mss)
  984. return -EINVAL;
  985. }
  986. /* Round if necessary so that SACKs cover only full MSSes
  987. * and/or the remaining small portion (if present)
  988. */
  989. if (pkt_len > mss) {
  990. unsigned int new_len = (pkt_len / mss) * mss;
  991. if (!in_sack && new_len < pkt_len) {
  992. new_len += mss;
  993. if (new_len > skb->len)
  994. return 0;
  995. }
  996. pkt_len = new_len;
  997. }
  998. err = tcp_fragment(sk, skb, pkt_len, mss);
  999. if (err < 0)
  1000. return err;
  1001. }
  1002. return in_sack;
  1003. }
  1004. /* Mark the given newly-SACKed range as such, adjusting counters and hints. */
  1005. static u8 tcp_sacktag_one(struct sock *sk,
  1006. struct tcp_sacktag_state *state, u8 sacked,
  1007. u32 start_seq, u32 end_seq,
  1008. bool dup_sack, int pcount)
  1009. {
  1010. struct tcp_sock *tp = tcp_sk(sk);
  1011. int fack_count = state->fack_count;
  1012. /* Account D-SACK for retransmitted packet. */
  1013. if (dup_sack && (sacked & TCPCB_RETRANS)) {
  1014. if (tp->undo_marker && tp->undo_retrans &&
  1015. after(end_seq, tp->undo_marker))
  1016. tp->undo_retrans--;
  1017. if (sacked & TCPCB_SACKED_ACKED)
  1018. state->reord = min(fack_count, state->reord);
  1019. }
  1020. /* Nothing to do; acked frame is about to be dropped (was ACKed). */
  1021. if (!after(end_seq, tp->snd_una))
  1022. return sacked;
  1023. if (!(sacked & TCPCB_SACKED_ACKED)) {
  1024. if (sacked & TCPCB_SACKED_RETRANS) {
  1025. /* If the segment is not tagged as lost,
  1026. * we do not clear RETRANS, believing
  1027. * that retransmission is still in flight.
  1028. */
  1029. if (sacked & TCPCB_LOST) {
  1030. sacked &= ~(TCPCB_LOST|TCPCB_SACKED_RETRANS);
  1031. tp->lost_out -= pcount;
  1032. tp->retrans_out -= pcount;
  1033. }
  1034. } else {
  1035. if (!(sacked & TCPCB_RETRANS)) {
  1036. /* New sack for not retransmitted frame,
  1037. * which was in hole. It is reordering.
  1038. */
  1039. if (before(start_seq,
  1040. tcp_highest_sack_seq(tp)))
  1041. state->reord = min(fack_count,
  1042. state->reord);
  1043. /* SACK enhanced F-RTO (RFC4138; Appendix B) */
  1044. if (!after(end_seq, tp->frto_highmark))
  1045. state->flag |= FLAG_ONLY_ORIG_SACKED;
  1046. }
  1047. if (sacked & TCPCB_LOST) {
  1048. sacked &= ~TCPCB_LOST;
  1049. tp->lost_out -= pcount;
  1050. }
  1051. }
  1052. sacked |= TCPCB_SACKED_ACKED;
  1053. state->flag |= FLAG_DATA_SACKED;
  1054. tp->sacked_out += pcount;
  1055. fack_count += pcount;
  1056. /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
  1057. if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) &&
  1058. before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
  1059. tp->lost_cnt_hint += pcount;
  1060. if (fack_count > tp->fackets_out)
  1061. tp->fackets_out = fack_count;
  1062. }
  1063. /* D-SACK. We can detect redundant retransmission in S|R and plain R
  1064. * frames and clear it. undo_retrans is decreased above, L|R frames
  1065. * are accounted above as well.
  1066. */
  1067. if (dup_sack && (sacked & TCPCB_SACKED_RETRANS)) {
  1068. sacked &= ~TCPCB_SACKED_RETRANS;
  1069. tp->retrans_out -= pcount;
  1070. }
  1071. return sacked;
  1072. }
  1073. /* Shift newly-SACKed bytes from this skb to the immediately previous
  1074. * already-SACKed sk_buff. Mark the newly-SACKed bytes as such.
  1075. */
  1076. static bool tcp_shifted_skb(struct sock *sk, struct sk_buff *skb,
  1077. struct tcp_sacktag_state *state,
  1078. unsigned int pcount, int shifted, int mss,
  1079. bool dup_sack)
  1080. {
  1081. struct tcp_sock *tp = tcp_sk(sk);
  1082. struct sk_buff *prev = tcp_write_queue_prev(sk, skb);
  1083. u32 start_seq = TCP_SKB_CB(skb)->seq; /* start of newly-SACKed */
  1084. u32 end_seq = start_seq + shifted; /* end of newly-SACKed */
  1085. BUG_ON(!pcount);
  1086. /* Adjust counters and hints for the newly sacked sequence
  1087. * range but discard the return value since prev is already
  1088. * marked. We must tag the range first because the seq
  1089. * advancement below implicitly advances
  1090. * tcp_highest_sack_seq() when skb is highest_sack.
  1091. */
  1092. tcp_sacktag_one(sk, state, TCP_SKB_CB(skb)->sacked,
  1093. start_seq, end_seq, dup_sack, pcount);
  1094. if (skb == tp->lost_skb_hint)
  1095. tp->lost_cnt_hint += pcount;
  1096. TCP_SKB_CB(prev)->end_seq += shifted;
  1097. TCP_SKB_CB(skb)->seq += shifted;
  1098. skb_shinfo(prev)->gso_segs += pcount;
  1099. BUG_ON(skb_shinfo(skb)->gso_segs < pcount);
  1100. skb_shinfo(skb)->gso_segs -= pcount;
  1101. /* When we're adding to gso_segs == 1, gso_size will be zero,
  1102. * in theory this shouldn't be necessary but as long as DSACK
  1103. * code can come after this skb later on it's better to keep
  1104. * setting gso_size to something.
  1105. */
  1106. if (!skb_shinfo(prev)->gso_size) {
  1107. skb_shinfo(prev)->gso_size = mss;
  1108. skb_shinfo(prev)->gso_type = sk->sk_gso_type;
  1109. }
  1110. /* CHECKME: To clear or not to clear? Mimics normal skb currently */
  1111. if (skb_shinfo(skb)->gso_segs <= 1) {
  1112. skb_shinfo(skb)->gso_size = 0;
  1113. skb_shinfo(skb)->gso_type = 0;
  1114. }
  1115. /* Difference in this won't matter, both ACKed by the same cumul. ACK */
  1116. TCP_SKB_CB(prev)->sacked |= (TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS);
  1117. if (skb->len > 0) {
  1118. BUG_ON(!tcp_skb_pcount(skb));
  1119. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTED);
  1120. return false;
  1121. }
  1122. /* Whole SKB was eaten :-) */
  1123. if (skb == tp->retransmit_skb_hint)
  1124. tp->retransmit_skb_hint = prev;
  1125. if (skb == tp->scoreboard_skb_hint)
  1126. tp->scoreboard_skb_hint = prev;
  1127. if (skb == tp->lost_skb_hint) {
  1128. tp->lost_skb_hint = prev;
  1129. tp->lost_cnt_hint -= tcp_skb_pcount(prev);
  1130. }
  1131. TCP_SKB_CB(skb)->tcp_flags |= TCP_SKB_CB(prev)->tcp_flags;
  1132. if (skb == tcp_highest_sack(sk))
  1133. tcp_advance_highest_sack(sk, skb);
  1134. tcp_unlink_write_queue(skb, sk);
  1135. sk_wmem_free_skb(sk, skb);
  1136. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKMERGED);
  1137. return true;
  1138. }
  1139. /* I wish gso_size would have a bit more sane initialization than
  1140. * something-or-zero which complicates things
  1141. */
  1142. static int tcp_skb_seglen(const struct sk_buff *skb)
  1143. {
  1144. return tcp_skb_pcount(skb) == 1 ? skb->len : tcp_skb_mss(skb);
  1145. }
  1146. /* Shifting pages past head area doesn't work */
  1147. static int skb_can_shift(const struct sk_buff *skb)
  1148. {
  1149. return !skb_headlen(skb) && skb_is_nonlinear(skb);
  1150. }
  1151. /* Try collapsing SACK blocks spanning across multiple skbs to a single
  1152. * skb.
  1153. */
  1154. static struct sk_buff *tcp_shift_skb_data(struct sock *sk, struct sk_buff *skb,
  1155. struct tcp_sacktag_state *state,
  1156. u32 start_seq, u32 end_seq,
  1157. bool dup_sack)
  1158. {
  1159. struct tcp_sock *tp = tcp_sk(sk);
  1160. struct sk_buff *prev;
  1161. int mss;
  1162. int pcount = 0;
  1163. int len;
  1164. int in_sack;
  1165. if (!sk_can_gso(sk))
  1166. goto fallback;
  1167. /* Normally R but no L won't result in plain S */
  1168. if (!dup_sack &&
  1169. (TCP_SKB_CB(skb)->sacked & (TCPCB_LOST|TCPCB_SACKED_RETRANS)) == TCPCB_SACKED_RETRANS)
  1170. goto fallback;
  1171. if (!skb_can_shift(skb))
  1172. goto fallback;
  1173. /* This frame is about to be dropped (was ACKed). */
  1174. if (!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una))
  1175. goto fallback;
  1176. /* Can only happen with delayed DSACK + discard craziness */
  1177. if (unlikely(skb == tcp_write_queue_head(sk)))
  1178. goto fallback;
  1179. prev = tcp_write_queue_prev(sk, skb);
  1180. if ((TCP_SKB_CB(prev)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED)
  1181. goto fallback;
  1182. in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq) &&
  1183. !before(end_seq, TCP_SKB_CB(skb)->end_seq);
  1184. if (in_sack) {
  1185. len = skb->len;
  1186. pcount = tcp_skb_pcount(skb);
  1187. mss = tcp_skb_seglen(skb);
  1188. /* TODO: Fix DSACKs to not fragment already SACKed and we can
  1189. * drop this restriction as unnecessary
  1190. */
  1191. if (mss != tcp_skb_seglen(prev))
  1192. goto fallback;
  1193. } else {
  1194. if (!after(TCP_SKB_CB(skb)->end_seq, start_seq))
  1195. goto noop;
  1196. /* CHECKME: This is non-MSS split case only?, this will
  1197. * cause skipped skbs due to advancing loop btw, original
  1198. * has that feature too
  1199. */
  1200. if (tcp_skb_pcount(skb) <= 1)
  1201. goto noop;
  1202. in_sack = !after(start_seq, TCP_SKB_CB(skb)->seq);
  1203. if (!in_sack) {
  1204. /* TODO: head merge to next could be attempted here
  1205. * if (!after(TCP_SKB_CB(skb)->end_seq, end_seq)),
  1206. * though it might not be worth of the additional hassle
  1207. *
  1208. * ...we can probably just fallback to what was done
  1209. * previously. We could try merging non-SACKed ones
  1210. * as well but it probably isn't going to buy off
  1211. * because later SACKs might again split them, and
  1212. * it would make skb timestamp tracking considerably
  1213. * harder problem.
  1214. */
  1215. goto fallback;
  1216. }
  1217. len = end_seq - TCP_SKB_CB(skb)->seq;
  1218. BUG_ON(len < 0);
  1219. BUG_ON(len > skb->len);
  1220. /* MSS boundaries should be honoured or else pcount will
  1221. * severely break even though it makes things bit trickier.
  1222. * Optimize common case to avoid most of the divides
  1223. */
  1224. mss = tcp_skb_mss(skb);
  1225. /* TODO: Fix DSACKs to not fragment already SACKed and we can
  1226. * drop this restriction as unnecessary
  1227. */
  1228. if (mss != tcp_skb_seglen(prev))
  1229. goto fallback;
  1230. if (len == mss) {
  1231. pcount = 1;
  1232. } else if (len < mss) {
  1233. goto noop;
  1234. } else {
  1235. pcount = len / mss;
  1236. len = pcount * mss;
  1237. }
  1238. }
  1239. /* tcp_sacktag_one() won't SACK-tag ranges below snd_una */
  1240. if (!after(TCP_SKB_CB(skb)->seq + len, tp->snd_una))
  1241. goto fallback;
  1242. if (!skb_shift(prev, skb, len))
  1243. goto fallback;
  1244. if (!tcp_shifted_skb(sk, skb, state, pcount, len, mss, dup_sack))
  1245. goto out;
  1246. /* Hole filled allows collapsing with the next as well, this is very
  1247. * useful when hole on every nth skb pattern happens
  1248. */
  1249. if (prev == tcp_write_queue_tail(sk))
  1250. goto out;
  1251. skb = tcp_write_queue_next(sk, prev);
  1252. if (!skb_can_shift(skb) ||
  1253. (skb == tcp_send_head(sk)) ||
  1254. ((TCP_SKB_CB(skb)->sacked & TCPCB_TAGBITS) != TCPCB_SACKED_ACKED) ||
  1255. (mss != tcp_skb_seglen(skb)))
  1256. goto out;
  1257. len = skb->len;
  1258. if (skb_shift(prev, skb, len)) {
  1259. pcount += tcp_skb_pcount(skb);
  1260. tcp_shifted_skb(sk, skb, state, tcp_skb_pcount(skb), len, mss, 0);
  1261. }
  1262. out:
  1263. state->fack_count += pcount;
  1264. return prev;
  1265. noop:
  1266. return skb;
  1267. fallback:
  1268. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_SACKSHIFTFALLBACK);
  1269. return NULL;
  1270. }
  1271. static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
  1272. struct tcp_sack_block *next_dup,
  1273. struct tcp_sacktag_state *state,
  1274. u32 start_seq, u32 end_seq,
  1275. bool dup_sack_in)
  1276. {
  1277. struct tcp_sock *tp = tcp_sk(sk);
  1278. struct sk_buff *tmp;
  1279. tcp_for_write_queue_from(skb, sk) {
  1280. int in_sack = 0;
  1281. bool dup_sack = dup_sack_in;
  1282. if (skb == tcp_send_head(sk))
  1283. break;
  1284. /* queue is in-order => we can short-circuit the walk early */
  1285. if (!before(TCP_SKB_CB(skb)->seq, end_seq))
  1286. break;
  1287. if ((next_dup != NULL) &&
  1288. before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
  1289. in_sack = tcp_match_skb_to_sack(sk, skb,
  1290. next_dup->start_seq,
  1291. next_dup->end_seq);
  1292. if (in_sack > 0)
  1293. dup_sack = true;
  1294. }
  1295. /* skb reference here is a bit tricky to get right, since
  1296. * shifting can eat and free both this skb and the next,
  1297. * so not even _safe variant of the loop is enough.
  1298. */
  1299. if (in_sack <= 0) {
  1300. tmp = tcp_shift_skb_data(sk, skb, state,
  1301. start_seq, end_seq, dup_sack);
  1302. if (tmp != NULL) {
  1303. if (tmp != skb) {
  1304. skb = tmp;
  1305. continue;
  1306. }
  1307. in_sack = 0;
  1308. } else {
  1309. in_sack = tcp_match_skb_to_sack(sk, skb,
  1310. start_seq,
  1311. end_seq);
  1312. }
  1313. }
  1314. if (unlikely(in_sack < 0))
  1315. break;
  1316. if (in_sack) {
  1317. TCP_SKB_CB(skb)->sacked =
  1318. tcp_sacktag_one(sk,
  1319. state,
  1320. TCP_SKB_CB(skb)->sacked,
  1321. TCP_SKB_CB(skb)->seq,
  1322. TCP_SKB_CB(skb)->end_seq,
  1323. dup_sack,
  1324. tcp_skb_pcount(skb));
  1325. if (!before(TCP_SKB_CB(skb)->seq,
  1326. tcp_highest_sack_seq(tp)))
  1327. tcp_advance_highest_sack(sk, skb);
  1328. }
  1329. state->fack_count += tcp_skb_pcount(skb);
  1330. }
  1331. return skb;
  1332. }
  1333. /* Avoid all extra work that is being done by sacktag while walking in
  1334. * a normal way
  1335. */
  1336. static struct sk_buff *tcp_sacktag_skip(struct sk_buff *skb, struct sock *sk,
  1337. struct tcp_sacktag_state *state,
  1338. u32 skip_to_seq)
  1339. {
  1340. tcp_for_write_queue_from(skb, sk) {
  1341. if (skb == tcp_send_head(sk))
  1342. break;
  1343. if (after(TCP_SKB_CB(skb)->end_seq, skip_to_seq))
  1344. break;
  1345. state->fack_count += tcp_skb_pcount(skb);
  1346. }
  1347. return skb;
  1348. }
  1349. static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
  1350. struct sock *sk,
  1351. struct tcp_sack_block *next_dup,
  1352. struct tcp_sacktag_state *state,
  1353. u32 skip_to_seq)
  1354. {
  1355. if (next_dup == NULL)
  1356. return skb;
  1357. if (before(next_dup->start_seq, skip_to_seq)) {
  1358. skb = tcp_sacktag_skip(skb, sk, state, next_dup->start_seq);
  1359. skb = tcp_sacktag_walk(skb, sk, NULL, state,
  1360. next_dup->start_seq, next_dup->end_seq,
  1361. 1);
  1362. }
  1363. return skb;
  1364. }
  1365. static int tcp_sack_cache_ok(const struct tcp_sock *tp, const struct tcp_sack_block *cache)
  1366. {
  1367. return cache < tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
  1368. }
  1369. static int
  1370. tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
  1371. u32 prior_snd_una)
  1372. {
  1373. const struct inet_connection_sock *icsk = inet_csk(sk);
  1374. struct tcp_sock *tp = tcp_sk(sk);
  1375. const unsigned char *ptr = (skb_transport_header(ack_skb) +
  1376. TCP_SKB_CB(ack_skb)->sacked);
  1377. struct tcp_sack_block_wire *sp_wire = (struct tcp_sack_block_wire *)(ptr+2);
  1378. struct tcp_sack_block sp[TCP_NUM_SACKS];
  1379. struct tcp_sack_block *cache;
  1380. struct tcp_sacktag_state state;
  1381. struct sk_buff *skb;
  1382. int num_sacks = min(TCP_NUM_SACKS, (ptr[1] - TCPOLEN_SACK_BASE) >> 3);
  1383. int used_sacks;
  1384. bool found_dup_sack = false;
  1385. int i, j;
  1386. int first_sack_index;
  1387. state.flag = 0;
  1388. state.reord = tp->packets_out;
  1389. if (!tp->sacked_out) {
  1390. if (WARN_ON(tp->fackets_out))
  1391. tp->fackets_out = 0;
  1392. tcp_highest_sack_reset(sk);
  1393. }
  1394. found_dup_sack = tcp_check_dsack(sk, ack_skb, sp_wire,
  1395. num_sacks, prior_snd_una);
  1396. if (found_dup_sack)
  1397. state.flag |= FLAG_DSACKING_ACK;
  1398. /* Eliminate too old ACKs, but take into
  1399. * account more or less fresh ones, they can
  1400. * contain valid SACK info.
  1401. */
  1402. if (before(TCP_SKB_CB(ack_skb)->ack_seq, prior_snd_una - tp->max_window))
  1403. return 0;
  1404. if (!tp->packets_out)
  1405. goto out;
  1406. used_sacks = 0;
  1407. first_sack_index = 0;
  1408. for (i = 0; i < num_sacks; i++) {
  1409. bool dup_sack = !i && found_dup_sack;
  1410. sp[used_sacks].start_seq = get_unaligned_be32(&sp_wire[i].start_seq);
  1411. sp[used_sacks].end_seq = get_unaligned_be32(&sp_wire[i].end_seq);
  1412. if (!tcp_is_sackblock_valid(tp, dup_sack,
  1413. sp[used_sacks].start_seq,
  1414. sp[used_sacks].end_seq)) {
  1415. int mib_idx;
  1416. if (dup_sack) {
  1417. if (!tp->undo_marker)
  1418. mib_idx = LINUX_MIB_TCPDSACKIGNOREDNOUNDO;
  1419. else
  1420. mib_idx = LINUX_MIB_TCPDSACKIGNOREDOLD;
  1421. } else {
  1422. /* Don't count olds caused by ACK reordering */
  1423. if ((TCP_SKB_CB(ack_skb)->ack_seq != tp->snd_una) &&
  1424. !after(sp[used_sacks].end_seq, tp->snd_una))
  1425. continue;
  1426. mib_idx = LINUX_MIB_TCPSACKDISCARD;
  1427. }
  1428. NET_INC_STATS_BH(sock_net(sk), mib_idx);
  1429. if (i == 0)
  1430. first_sack_index = -1;
  1431. continue;
  1432. }
  1433. /* Ignore very old stuff early */
  1434. if (!after(sp[used_sacks].end_seq, prior_snd_una))
  1435. continue;
  1436. used_sacks++;
  1437. }
  1438. /* order SACK blocks to allow in order walk of the retrans queue */
  1439. for (i = used_sacks - 1; i > 0; i--) {
  1440. for (j = 0; j < i; j++) {
  1441. if (after(sp[j].start_seq, sp[j + 1].start_seq)) {
  1442. swap(sp[j], sp[j + 1]);
  1443. /* Track where the first SACK block goes to */
  1444. if (j == first_sack_index)
  1445. first_sack_index = j + 1;
  1446. }
  1447. }
  1448. }
  1449. skb = tcp_write_queue_head(sk);
  1450. state.fack_count = 0;
  1451. i = 0;
  1452. if (!tp->sacked_out) {
  1453. /* It's already past, so skip checking against it */
  1454. cache = tp->recv_sack_cache + ARRAY_SIZE(tp->recv_sack_cache);
  1455. } else {
  1456. cache = tp->recv_sack_cache;
  1457. /* Skip empty blocks in at head of the cache */
  1458. while (tcp_sack_cache_ok(tp, cache) && !cache->start_seq &&
  1459. !cache->end_seq)
  1460. cache++;
  1461. }
  1462. while (i < used_sacks) {
  1463. u32 start_seq = sp[i].start_seq;
  1464. u32 end_seq = sp[i].end_seq;
  1465. bool dup_sack = (found_dup_sack && (i == first_sack_index));
  1466. struct tcp_sack_block *next_dup = NULL;
  1467. if (found_dup_sack && ((i + 1) == first_sack_index))
  1468. next_dup = &sp[i + 1];
  1469. /* Skip too early cached blocks */
  1470. while (tcp_sack_cache_ok(tp, cache) &&
  1471. !before(start_seq, cache->end_seq))
  1472. cache++;
  1473. /* Can skip some work by looking recv_sack_cache? */
  1474. if (tcp_sack_cache_ok(tp, cache) && !dup_sack &&
  1475. after(end_seq, cache->start_seq)) {
  1476. /* Head todo? */
  1477. if (before(start_seq, cache->start_seq)) {
  1478. skb = tcp_sacktag_skip(skb, sk, &state,
  1479. start_seq);
  1480. skb = tcp_sacktag_walk(skb, sk, next_dup,
  1481. &state,
  1482. start_seq,
  1483. cache->start_seq,
  1484. dup_sack);
  1485. }
  1486. /* Rest of the block already fully processed? */
  1487. if (!after(end_seq, cache->end_seq))
  1488. goto advance_sp;
  1489. skb = tcp_maybe_skipping_dsack(skb, sk, next_dup,
  1490. &state,
  1491. cache->end_seq);
  1492. /* ...tail remains todo... */
  1493. if (tcp_highest_sack_seq(tp) == cache->end_seq) {
  1494. /* ...but better entrypoint exists! */
  1495. skb = tcp_highest_sack(sk);
  1496. if (skb == NULL)
  1497. break;
  1498. state.fack_count = tp->fackets_out;
  1499. cache++;
  1500. goto walk;
  1501. }
  1502. skb = tcp_sacktag_skip(skb, sk, &state, cache->end_seq);
  1503. /* Check overlap against next cached too (past this one already) */
  1504. cache++;
  1505. continue;
  1506. }
  1507. if (!before(start_seq, tcp_highest_sack_seq(tp))) {
  1508. skb = tcp_highest_sack(sk);
  1509. if (skb == NULL)
  1510. break;
  1511. state.fack_count = tp->fackets_out;
  1512. }
  1513. skb = tcp_sacktag_skip(skb, sk, &state, start_seq);
  1514. walk:
  1515. skb = tcp_sacktag_walk(skb, sk, next_dup, &state,
  1516. start_seq, end_seq, dup_sack);
  1517. advance_sp:
  1518. /* SACK enhanced FRTO (RFC4138, Appendix B): Clearing correct
  1519. * due to in-order walk
  1520. */
  1521. if (after(end_seq, tp->frto_highmark))
  1522. state.flag &= ~FLAG_ONLY_ORIG_SACKED;
  1523. i++;
  1524. }
  1525. /* Clear the head of the cache sack blocks so we can skip it next time */
  1526. for (i = 0; i < ARRAY_SIZE(tp->recv_sack_cache) - used_sacks; i++) {
  1527. tp->recv_sack_cache[i].start_seq = 0;
  1528. tp->recv_sack_cache[i].end_seq = 0;
  1529. }
  1530. for (j = 0; j < used_sacks; j++)
  1531. tp->recv_sack_cache[i++] = sp[j];
  1532. tcp_mark_lost_retrans(sk);
  1533. tcp_verify_left_out(tp);
  1534. if ((state.reord < tp->fackets_out) &&
  1535. ((icsk->icsk_ca_state != TCP_CA_Loss) || tp->undo_marker) &&
  1536. (!tp->frto_highmark || after(tp->snd_una, tp->frto_highmark)))
  1537. tcp_update_reordering(sk, tp->fackets_out - state.reord, 0);
  1538. out:
  1539. #if FASTRETRANS_DEBUG > 0
  1540. WARN_ON((int)tp->sacked_out < 0);
  1541. WARN_ON((int)tp->lost_out < 0);
  1542. WARN_ON((int)tp->retrans_out < 0);
  1543. WARN_ON((int)tcp_packets_in_flight(tp) < 0);
  1544. #endif
  1545. return state.flag;
  1546. }
  1547. /* Limits sacked_out so that sum with lost_out isn't ever larger than
  1548. * packets_out. Returns false if sacked_out adjustement wasn't necessary.
  1549. */
  1550. static bool tcp_limit_reno_sacked(struct tcp_sock *tp)
  1551. {
  1552. u32 holes;
  1553. holes = max(tp->lost_out, 1U);
  1554. holes = min(holes, tp->packets_out);
  1555. if ((tp->sacked_out + holes) > tp->packets_out) {
  1556. tp->sacked_out = tp->packets_out - holes;
  1557. return true;
  1558. }
  1559. return false;
  1560. }
  1561. /* If we receive more dupacks than we expected counting segments
  1562. * in assumption of absent reordering, interpret this as reordering.
  1563. * The only another reason could be bug in receiver TCP.
  1564. */
  1565. static void tcp_check_reno_reordering(struct sock *sk, const int addend)
  1566. {
  1567. struct tcp_sock *tp = tcp_sk(sk);
  1568. if (tcp_limit_reno_sacked(tp))
  1569. tcp_update_reordering(sk, tp->packets_out + addend, 0);
  1570. }
  1571. /* Emulate SACKs for SACKless connection: account for a new dupack. */
  1572. static void tcp_add_reno_sack(struct sock *sk)
  1573. {
  1574. struct tcp_sock *tp = tcp_sk(sk);
  1575. tp->sacked_out++;
  1576. tcp_check_reno_reordering(sk, 0);
  1577. tcp_verify_left_out(tp);
  1578. }
  1579. /* Account for ACK, ACKing some data in Reno Recovery phase. */
  1580. static void tcp_remove_reno_sacks(struct sock *sk, int acked)
  1581. {
  1582. struct tcp_sock *tp = tcp_sk(sk);
  1583. if (acked > 0) {
  1584. /* One ACK acked hole. The rest eat duplicate ACKs. */
  1585. if (acked - 1 >= tp->sacked_out)
  1586. tp->sacked_out = 0;
  1587. else
  1588. tp->sacked_out -= acked - 1;
  1589. }
  1590. tcp_check_reno_reordering(sk, acked);
  1591. tcp_verify_left_out(tp);
  1592. }
  1593. static inline void tcp_reset_reno_sack(struct tcp_sock *tp)
  1594. {
  1595. tp->sacked_out = 0;
  1596. }
  1597. static int tcp_is_sackfrto(const struct tcp_sock *tp)
  1598. {
  1599. return (sysctl_tcp_frto == 0x2) && !tcp_is_reno(tp);
  1600. }
  1601. /* F-RTO can only be used if TCP has never retransmitted anything other than
  1602. * head (SACK enhanced variant from Appendix B of RFC4138 is more robust here)
  1603. */
  1604. bool tcp_use_frto(struct sock *sk)
  1605. {
  1606. const struct tcp_sock *tp = tcp_sk(sk);
  1607. const struct inet_connection_sock *icsk = inet_csk(sk);
  1608. struct sk_buff *skb;
  1609. if (!sysctl_tcp_frto)
  1610. return false;
  1611. /* MTU probe and F-RTO won't really play nicely along currently */
  1612. if (icsk->icsk_mtup.probe_size)
  1613. return false;
  1614. if (tcp_is_sackfrto(tp))
  1615. return true;
  1616. /* Avoid expensive walking of rexmit queue if possible */
  1617. if (tp->retrans_out > 1)
  1618. return false;
  1619. skb = tcp_write_queue_head(sk);
  1620. if (tcp_skb_is_last(sk, skb))
  1621. return true;
  1622. skb = tcp_write_queue_next(sk, skb); /* Skips head */
  1623. tcp_for_write_queue_from(skb, sk) {
  1624. if (skb == tcp_send_head(sk))
  1625. break;
  1626. if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
  1627. return false;
  1628. /* Short-circuit when first non-SACKed skb has been checked */
  1629. if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
  1630. break;
  1631. }
  1632. return true;
  1633. }
  1634. /* RTO occurred, but do not yet enter Loss state. Instead, defer RTO
  1635. * recovery a bit and use heuristics in tcp_process_frto() to detect if
  1636. * the RTO was spurious. Only clear SACKED_RETRANS of the head here to
  1637. * keep retrans_out counting accurate (with SACK F-RTO, other than head
  1638. * may still have that bit set); TCPCB_LOST and remaining SACKED_RETRANS
  1639. * bits are handled if the Loss state is really to be entered (in
  1640. * tcp_enter_frto_loss).
  1641. *
  1642. * Do like tcp_enter_loss() would; when RTO expires the second time it
  1643. * does:
  1644. * "Reduce ssthresh if it has not yet been made inside this window."
  1645. */
  1646. void tcp_enter_frto(struct sock *sk)
  1647. {
  1648. const struct inet_connection_sock *icsk = inet_csk(sk);
  1649. struct tcp_sock *tp = tcp_sk(sk);
  1650. struct sk_buff *skb;
  1651. if ((!tp->frto_counter && icsk->icsk_ca_state <= TCP_CA_Disorder) ||
  1652. tp->snd_una == tp->high_seq ||
  1653. ((icsk->icsk_ca_state == TCP_CA_Loss || tp->frto_counter) &&
  1654. !icsk->icsk_retransmits)) {
  1655. tp->prior_ssthresh = tcp_current_ssthresh(sk);
  1656. /* Our state is too optimistic in ssthresh() call because cwnd
  1657. * is not reduced until tcp_enter_frto_loss() when previous F-RTO
  1658. * recovery has not yet completed. Pattern would be this: RTO,
  1659. * Cumulative ACK, RTO (2xRTO for the same segment does not end
  1660. * up here twice).
  1661. * RFC4138 should be more specific on what to do, even though
  1662. * RTO is quite unlikely to occur after the first Cumulative ACK
  1663. * due to back-off and complexity of triggering events ...
  1664. */
  1665. if (tp->frto_counter) {
  1666. u32 stored_cwnd;
  1667. stored_cwnd = tp->snd_cwnd;
  1668. tp->snd_cwnd = 2;
  1669. tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
  1670. tp->snd_cwnd = stored_cwnd;
  1671. } else {
  1672. tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
  1673. }
  1674. /* ... in theory, cong.control module could do "any tricks" in
  1675. * ssthresh(), which means that ca_state, lost bits and lost_out
  1676. * counter would have to be faked before the call occurs. We
  1677. * consider that too expensive, unlikely and hacky, so modules
  1678. * using these in ssthresh() must deal these incompatibility
  1679. * issues if they receives CA_EVENT_FRTO and frto_counter != 0
  1680. */
  1681. tcp_ca_event(sk, CA_EVENT_FRTO);
  1682. }
  1683. tp->undo_marker = tp->snd_una;
  1684. tp->undo_retrans = 0;
  1685. skb = tcp_write_queue_head(sk);
  1686. if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
  1687. tp->undo_marker = 0;
  1688. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
  1689. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  1690. tp->retrans_out -= tcp_skb_pcount(skb);
  1691. }
  1692. tcp_verify_left_out(tp);
  1693. /* Too bad if TCP was application limited */
  1694. tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp) + 1);
  1695. /* Earlier loss recovery underway (see RFC4138; Appendix B).
  1696. * The last condition is necessary at least in tp->frto_counter case.
  1697. */
  1698. if (tcp_is_sackfrto(tp) && (tp->frto_counter ||
  1699. ((1 << icsk->icsk_ca_state) & (TCPF_CA_Recovery|TCPF_CA_Loss))) &&
  1700. after(tp->high_seq, tp->snd_una)) {
  1701. tp->frto_highmark = tp->high_seq;
  1702. } else {
  1703. tp->frto_highmark = tp->snd_nxt;
  1704. }
  1705. tcp_set_ca_state(sk, TCP_CA_Disorder);
  1706. tp->high_seq = tp->snd_nxt;
  1707. tp->frto_counter = 1;
  1708. }
  1709. /* Enter Loss state after F-RTO was applied. Dupack arrived after RTO,
  1710. * which indicates that we should follow the traditional RTO recovery,
  1711. * i.e. mark everything lost and do go-back-N retransmission.
  1712. */
  1713. static void tcp_enter_frto_loss(struct sock *sk, int allowed_segments, int flag)
  1714. {
  1715. struct tcp_sock *tp = tcp_sk(sk);
  1716. struct sk_buff *skb;
  1717. tp->lost_out = 0;
  1718. tp->retrans_out = 0;
  1719. if (tcp_is_reno(tp))
  1720. tcp_reset_reno_sack(tp);
  1721. tcp_for_write_queue(skb, sk) {
  1722. if (skb == tcp_send_head(sk))
  1723. break;
  1724. TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
  1725. /*
  1726. * Count the retransmission made on RTO correctly (only when
  1727. * waiting for the first ACK and did not get it)...
  1728. */
  1729. if ((tp->frto_counter == 1) && !(flag & FLAG_DATA_ACKED)) {
  1730. /* For some reason this R-bit might get cleared? */
  1731. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS)
  1732. tp->retrans_out += tcp_skb_pcount(skb);
  1733. /* ...enter this if branch just for the first segment */
  1734. flag |= FLAG_DATA_ACKED;
  1735. } else {
  1736. if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
  1737. tp->undo_marker = 0;
  1738. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  1739. }
  1740. /* Marking forward transmissions that were made after RTO lost
  1741. * can cause unnecessary retransmissions in some scenarios,
  1742. * SACK blocks will mitigate that in some but not in all cases.
  1743. * We used to not mark them but it was causing break-ups with
  1744. * receivers that do only in-order receival.
  1745. *
  1746. * TODO: we could detect presence of such receiver and select
  1747. * different behavior per flow.
  1748. */
  1749. if (!(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
  1750. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  1751. tp->lost_out += tcp_skb_pcount(skb);
  1752. tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
  1753. }
  1754. }
  1755. tcp_verify_left_out(tp);
  1756. tp->snd_cwnd = tcp_packets_in_flight(tp) + allowed_segments;
  1757. tp->snd_cwnd_cnt = 0;
  1758. tp->snd_cwnd_stamp = tcp_time_stamp;
  1759. tp->frto_counter = 0;
  1760. tp->bytes_acked = 0;
  1761. tp->reordering = min_t(unsigned int, tp->reordering,
  1762. sysctl_tcp_reordering);
  1763. tcp_set_ca_state(sk, TCP_CA_Loss);
  1764. tp->high_seq = tp->snd_nxt;
  1765. TCP_ECN_queue_cwr(tp);
  1766. tcp_clear_all_retrans_hints(tp);
  1767. }
  1768. static void tcp_clear_retrans_partial(struct tcp_sock *tp)
  1769. {
  1770. tp->retrans_out = 0;
  1771. tp->lost_out = 0;
  1772. tp->undo_marker = 0;
  1773. tp->undo_retrans = 0;
  1774. }
  1775. void tcp_clear_retrans(struct tcp_sock *tp)
  1776. {
  1777. tcp_clear_retrans_partial(tp);
  1778. tp->fackets_out = 0;
  1779. tp->sacked_out = 0;
  1780. }
  1781. /* Enter Loss state. If "how" is not zero, forget all SACK information
  1782. * and reset tags completely, otherwise preserve SACKs. If receiver
  1783. * dropped its ofo queue, we will know this due to reneging detection.
  1784. */
  1785. void tcp_enter_loss(struct sock *sk, int how)
  1786. {
  1787. const struct inet_connection_sock *icsk = inet_csk(sk);
  1788. struct tcp_sock *tp = tcp_sk(sk);
  1789. struct sk_buff *skb;
  1790. /* Reduce ssthresh if it has not yet been made inside this window. */
  1791. if (icsk->icsk_ca_state <= TCP_CA_Disorder || tp->snd_una == tp->high_seq ||
  1792. (icsk->icsk_ca_state == TCP_CA_Loss && !icsk->icsk_retransmits)) {
  1793. tp->prior_ssthresh = tcp_current_ssthresh(sk);
  1794. tp->snd_ssthresh = icsk->icsk_ca_ops->ssthresh(sk);
  1795. tcp_ca_event(sk, CA_EVENT_LOSS);
  1796. }
  1797. tp->snd_cwnd = 1;
  1798. tp->snd_cwnd_cnt = 0;
  1799. tp->snd_cwnd_stamp = tcp_time_stamp;
  1800. tp->bytes_acked = 0;
  1801. tcp_clear_retrans_partial(tp);
  1802. if (tcp_is_reno(tp))
  1803. tcp_reset_reno_sack(tp);
  1804. if (!how) {
  1805. /* Push undo marker, if it was plain RTO and nothing
  1806. * was retransmitted. */
  1807. tp->undo_marker = tp->snd_una;
  1808. } else {
  1809. tp->sacked_out = 0;
  1810. tp->fackets_out = 0;
  1811. }
  1812. tcp_clear_all_retrans_hints(tp);
  1813. tcp_for_write_queue(skb, sk) {
  1814. if (skb == tcp_send_head(sk))
  1815. break;
  1816. if (TCP_SKB_CB(skb)->sacked & TCPCB_RETRANS)
  1817. tp->undo_marker = 0;
  1818. TCP_SKB_CB(skb)->sacked &= (~TCPCB_TAGBITS)|TCPCB_SACKED_ACKED;
  1819. if (!(TCP_SKB_CB(skb)->sacked&TCPCB_SACKED_ACKED) || how) {
  1820. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_ACKED;
  1821. TCP_SKB_CB(skb)->sacked |= TCPCB_LOST;
  1822. tp->lost_out += tcp_skb_pcount(skb);
  1823. tp->retransmit_high = TCP_SKB_CB(skb)->end_seq;
  1824. }
  1825. }
  1826. tcp_verify_left_out(tp);
  1827. tp->reordering = min_t(unsigned int, tp->reordering,
  1828. sysctl_tcp_reordering);
  1829. tcp_set_ca_state(sk, TCP_CA_Loss);
  1830. tp->high_seq = tp->snd_nxt;
  1831. TCP_ECN_queue_cwr(tp);
  1832. /* Abort F-RTO algorithm if one is in progress */
  1833. tp->frto_counter = 0;
  1834. }
  1835. /* If ACK arrived pointing to a remembered SACK, it means that our
  1836. * remembered SACKs do not reflect real state of receiver i.e.
  1837. * receiver _host_ is heavily congested (or buggy).
  1838. *
  1839. * Do processing similar to RTO timeout.
  1840. */
  1841. static bool tcp_check_sack_reneging(struct sock *sk, int flag)
  1842. {
  1843. if (flag & FLAG_SACK_RENEGING) {
  1844. struct inet_connection_sock *icsk = inet_csk(sk);
  1845. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSACKRENEGING);
  1846. tcp_enter_loss(sk, 1);
  1847. icsk->icsk_retransmits++;
  1848. tcp_retransmit_skb(sk, tcp_write_queue_head(sk));
  1849. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  1850. icsk->icsk_rto, TCP_RTO_MAX);
  1851. return true;
  1852. }
  1853. return false;
  1854. }
  1855. static inline int tcp_fackets_out(const struct tcp_sock *tp)
  1856. {
  1857. return tcp_is_reno(tp) ? tp->sacked_out + 1 : tp->fackets_out;
  1858. }
  1859. /* Heurestics to calculate number of duplicate ACKs. There's no dupACKs
  1860. * counter when SACK is enabled (without SACK, sacked_out is used for
  1861. * that purpose).
  1862. *
  1863. * Instead, with FACK TCP uses fackets_out that includes both SACKed
  1864. * segments up to the highest received SACK block so far and holes in
  1865. * between them.
  1866. *
  1867. * With reordering, holes may still be in flight, so RFC3517 recovery
  1868. * uses pure sacked_out (total number of SACKed segments) even though
  1869. * it violates the RFC that uses duplicate ACKs, often these are equal
  1870. * but when e.g. out-of-window ACKs or packet duplication occurs,
  1871. * they differ. Since neither occurs due to loss, TCP should really
  1872. * ignore them.
  1873. */
  1874. static inline int tcp_dupack_heuristics(const struct tcp_sock *tp)
  1875. {
  1876. return tcp_is_fack(tp) ? tp->fackets_out : tp->sacked_out + 1;
  1877. }
  1878. static bool tcp_pause_early_retransmit(struct sock *sk, int flag)
  1879. {
  1880. struct tcp_sock *tp = tcp_sk(sk);
  1881. unsigned long delay;
  1882. /* Delay early retransmit and entering fast recovery for
  1883. * max(RTT/4, 2msec) unless ack has ECE mark, no RTT samples
  1884. * available, or RTO is scheduled to fire first.
  1885. */
  1886. if (sysctl_tcp_early_retrans < 2 || (flag & FLAG_ECE) || !tp->srtt)
  1887. return false;
  1888. delay = max_t(unsigned long, (tp->srtt >> 5), msecs_to_jiffies(2));
  1889. if (!time_after(inet_csk(sk)->icsk_timeout, (jiffies + delay)))
  1890. return false;
  1891. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, delay, TCP_RTO_MAX);
  1892. tp->early_retrans_delayed = 1;
  1893. return true;
  1894. }
  1895. static inline int tcp_skb_timedout(const struct sock *sk,
  1896. const struct sk_buff *skb)
  1897. {
  1898. return tcp_time_stamp - TCP_SKB_CB(skb)->when > inet_csk(sk)->icsk_rto;
  1899. }
  1900. static inline int tcp_head_timedout(const struct sock *sk)
  1901. {
  1902. const struct tcp_sock *tp = tcp_sk(sk);
  1903. return tp->packets_out &&
  1904. tcp_skb_timedout(sk, tcp_write_queue_head(sk));
  1905. }
  1906. /* Linux NewReno/SACK/FACK/ECN state machine.
  1907. * --------------------------------------
  1908. *
  1909. * "Open" Normal state, no dubious events, fast path.
  1910. * "Disorder" In all the respects it is "Open",
  1911. * but requires a bit more attention. It is entered when
  1912. * we see some SACKs or dupacks. It is split of "Open"
  1913. * mainly to move some processing from fast path to slow one.
  1914. * "CWR" CWND was reduced due to some Congestion Notification event.
  1915. * It can be ECN, ICMP source quench, local device congestion.
  1916. * "Recovery" CWND was reduced, we are fast-retransmitting.
  1917. * "Loss" CWND was reduced due to RTO timeout or SACK reneging.
  1918. *
  1919. * tcp_fastretrans_alert() is entered:
  1920. * - each incoming ACK, if state is not "Open"
  1921. * - when arrived ACK is unusual, namely:
  1922. * * SACK
  1923. * * Duplicate ACK.
  1924. * * ECN ECE.
  1925. *
  1926. * Counting packets in flight is pretty simple.
  1927. *
  1928. * in_flight = packets_out - left_out + retrans_out
  1929. *
  1930. * packets_out is SND.NXT-SND.UNA counted in packets.
  1931. *
  1932. * retrans_out is number of retransmitted segments.
  1933. *
  1934. * left_out is number of segments left network, but not ACKed yet.
  1935. *
  1936. * left_out = sacked_out + lost_out
  1937. *
  1938. * sacked_out: Packets, which arrived to receiver out of order
  1939. * and hence not ACKed. With SACKs this number is simply
  1940. * amount of SACKed data. Even without SACKs
  1941. * it is easy to give pretty reliable estimate of this number,
  1942. * counting duplicate ACKs.
  1943. *
  1944. * lost_out: Packets lost by network. TCP has no explicit
  1945. * "loss notification" feedback from network (for now).
  1946. * It means that this number can be only _guessed_.
  1947. * Actually, it is the heuristics to predict lossage that
  1948. * distinguishes different algorithms.
  1949. *
  1950. * F.e. after RTO, when all the queue is considered as lost,
  1951. * lost_out = packets_out and in_flight = retrans_out.
  1952. *
  1953. * Essentially, we have now two algorithms counting
  1954. * lost packets.
  1955. *
  1956. * FACK: It is the simplest heuristics. As soon as we decided
  1957. * that something is lost, we decide that _all_ not SACKed
  1958. * packets until the most forward SACK are lost. I.e.
  1959. * lost_out = fackets_out - sacked_out and left_out = fackets_out.
  1960. * It is absolutely correct estimate, if network does not reorder
  1961. * packets. And it loses any connection to reality when reordering
  1962. * takes place. We use FACK by default until reordering
  1963. * is suspected on the path to this destination.
  1964. *
  1965. * NewReno: when Recovery is entered, we assume that one segment
  1966. * is lost (classic Reno). While we are in Recovery and
  1967. * a partial ACK arrives, we assume that one more packet
  1968. * is lost (NewReno). This heuristics are the same in NewReno
  1969. * and SACK.
  1970. *
  1971. * Imagine, that's all! Forget about all this shamanism about CWND inflation
  1972. * deflation etc. CWND is real congestion window, never inflated, changes
  1973. * only according to classic VJ rules.
  1974. *
  1975. * Really tricky (and requiring careful tuning) part of algorithm
  1976. * is hidden in functions tcp_time_to_recover() and tcp_xmit_retransmit_queue().
  1977. * The first determines the moment _when_ we should reduce CWND and,
  1978. * hence, slow down forward transmission. In fact, it determines the moment
  1979. * when we decide that hole is caused by loss, rather than by a reorder.
  1980. *
  1981. * tcp_xmit_retransmit_queue() decides, _what_ we should retransmit to fill
  1982. * holes, caused by lost packets.
  1983. *
  1984. * And the most logically complicated part of algorithm is undo
  1985. * heuristics. We detect false retransmits due to both too early
  1986. * fast retransmit (reordering) and underestimated RTO, analyzing
  1987. * timestamps and D-SACKs. When we detect that some segments were
  1988. * retransmitted by mistake and CWND reduction was wrong, we undo
  1989. * window reduction and abort recovery phase. This logic is hidden
  1990. * inside several functions named tcp_try_undo_<something>.
  1991. */
  1992. /* This function decides, when we should leave Disordered state
  1993. * and enter Recovery phase, reducing congestion window.
  1994. *
  1995. * Main question: may we further continue forward transmission
  1996. * with the same cwnd?
  1997. */
  1998. static bool tcp_time_to_recover(struct sock *sk, int flag)
  1999. {
  2000. struct tcp_sock *tp = tcp_sk(sk);
  2001. __u32 packets_out;
  2002. /* Do not perform any recovery during F-RTO algorithm */
  2003. if (tp->frto_counter)
  2004. return false;
  2005. /* Trick#1: The loss is proven. */
  2006. if (tp->lost_out)
  2007. return true;
  2008. /* Not-A-Trick#2 : Classic rule... */
  2009. if (tcp_dupack_heuristics(tp) > tp->reordering)
  2010. return true;
  2011. /* Trick#3 : when we use RFC2988 timer restart, fast
  2012. * retransmit can be triggered by timeout of queue head.
  2013. */
  2014. if (tcp_is_fack(tp) && tcp_head_timedout(sk))
  2015. return true;
  2016. /* Trick#4: It is still not OK... But will it be useful to delay
  2017. * recovery more?
  2018. */
  2019. packets_out = tp->packets_out;
  2020. if (packets_out <= tp->reordering &&
  2021. tp->sacked_out >= max_t(__u32, packets_out/2, sysctl_tcp_reordering) &&
  2022. !tcp_may_send_now(sk)) {
  2023. /* We have nothing to send. This connection is limited
  2024. * either by receiver window or by application.
  2025. */
  2026. return true;
  2027. }
  2028. /* If a thin stream is detected, retransmit after first
  2029. * received dupack. Employ only if SACK is supported in order
  2030. * to avoid possible corner-case series of spurious retransmissions
  2031. * Use only if there are no unsent data.
  2032. */
  2033. if ((tp->thin_dupack || sysctl_tcp_thin_dupack) &&
  2034. tcp_stream_is_thin(tp) && tcp_dupack_heuristics(tp) > 1 &&
  2035. tcp_is_sack(tp) && !tcp_send_head(sk))
  2036. return true;
  2037. /* Trick#6: TCP early retransmit, per RFC5827. To avoid spurious
  2038. * retransmissions due to small network reorderings, we implement
  2039. * Mitigation A.3 in the RFC and delay the retransmission for a short
  2040. * interval if appropriate.
  2041. */
  2042. if (tp->do_early_retrans && !tp->retrans_out && tp->sacked_out &&
  2043. (tp->packets_out == (tp->sacked_out + 1) && tp->packets_out < 4) &&
  2044. !tcp_may_send_now(sk))
  2045. return !tcp_pause_early_retransmit(sk, flag);
  2046. return false;
  2047. }
  2048. /* New heuristics: it is possible only after we switched to restart timer
  2049. * each time when something is ACKed. Hence, we can detect timed out packets
  2050. * during fast retransmit without falling to slow start.
  2051. *
  2052. * Usefulness of this as is very questionable, since we should know which of
  2053. * the segments is the next to timeout which is relatively expensive to find
  2054. * in general case unless we add some data structure just for that. The
  2055. * current approach certainly won't find the right one too often and when it
  2056. * finally does find _something_ it usually marks large part of the window
  2057. * right away (because a retransmission with a larger timestamp blocks the
  2058. * loop from advancing). -ij
  2059. */
  2060. static void tcp_timeout_skbs(struct sock *sk)
  2061. {
  2062. struct tcp_sock *tp = tcp_sk(sk);
  2063. struct sk_buff *skb;
  2064. if (!tcp_is_fack(tp) || !tcp_head_timedout(sk))
  2065. return;
  2066. skb = tp->scoreboard_skb_hint;
  2067. if (tp->scoreboard_skb_hint == NULL)
  2068. skb = tcp_write_queue_head(sk);
  2069. tcp_for_write_queue_from(skb, sk) {
  2070. if (skb == tcp_send_head(sk))
  2071. break;
  2072. if (!tcp_skb_timedout(sk, skb))
  2073. break;
  2074. tcp_skb_mark_lost(tp, skb);
  2075. }
  2076. tp->scoreboard_skb_hint = skb;
  2077. tcp_verify_left_out(tp);
  2078. }
  2079. /* Detect loss in event "A" above by marking head of queue up as lost.
  2080. * For FACK or non-SACK(Reno) senders, the first "packets" number of segments
  2081. * are considered lost. For RFC3517 SACK, a segment is considered lost if it
  2082. * has at least tp->reordering SACKed seqments above it; "packets" refers to
  2083. * the maximum SACKed segments to pass before reaching this limit.
  2084. */
  2085. static void tcp_mark_head_lost(struct sock *sk, int packets, int mark_head)
  2086. {
  2087. struct tcp_sock *tp = tcp_sk(sk);
  2088. struct sk_buff *skb;
  2089. int cnt, oldcnt;
  2090. int err;
  2091. unsigned int mss;
  2092. /* Use SACK to deduce losses of new sequences sent during recovery */
  2093. const u32 loss_high = tcp_is_sack(tp) ? tp->snd_nxt : tp->high_seq;
  2094. WARN_ON(packets > tp->packets_out);
  2095. if (tp->lost_skb_hint) {
  2096. skb = tp->lost_skb_hint;
  2097. cnt = tp->lost_cnt_hint;
  2098. /* Head already handled? */
  2099. if (mark_head && skb != tcp_write_queue_head(sk))
  2100. return;
  2101. } else {
  2102. skb = tcp_write_queue_head(sk);
  2103. cnt = 0;
  2104. }
  2105. tcp_for_write_queue_from(skb, sk) {
  2106. if (skb == tcp_send_head(sk))
  2107. break;
  2108. /* TODO: do this better */
  2109. /* this is not the most efficient way to do this... */
  2110. tp->lost_skb_hint = skb;
  2111. tp->lost_cnt_hint = cnt;
  2112. if (after(TCP_SKB_CB(skb)->end_seq, loss_high))
  2113. break;
  2114. oldcnt = cnt;
  2115. if (tcp_is_fack(tp) || tcp_is_reno(tp) ||
  2116. (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
  2117. cnt += tcp_skb_pcount(skb);
  2118. if (cnt > packets) {
  2119. if ((tcp_is_sack(tp) && !tcp_is_fack(tp)) ||
  2120. (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED) ||
  2121. (oldcnt >= packets))
  2122. break;
  2123. mss = skb_shinfo(skb)->gso_size;
  2124. err = tcp_fragment(sk, skb, (packets - oldcnt) * mss, mss);
  2125. if (err < 0)
  2126. break;
  2127. cnt = packets;
  2128. }
  2129. tcp_skb_mark_lost(tp, skb);
  2130. if (mark_head)
  2131. break;
  2132. }
  2133. tcp_verify_left_out(tp);
  2134. }
  2135. /* Account newly detected lost packet(s) */
  2136. static void tcp_update_scoreboard(struct sock *sk, int fast_rexmit)
  2137. {
  2138. struct tcp_sock *tp = tcp_sk(sk);
  2139. if (tcp_is_reno(tp)) {
  2140. tcp_mark_head_lost(sk, 1, 1);
  2141. } else if (tcp_is_fack(tp)) {
  2142. int lost = tp->fackets_out - tp->reordering;
  2143. if (lost <= 0)
  2144. lost = 1;
  2145. tcp_mark_head_lost(sk, lost, 0);
  2146. } else {
  2147. int sacked_upto = tp->sacked_out - tp->reordering;
  2148. if (sacked_upto >= 0)
  2149. tcp_mark_head_lost(sk, sacked_upto, 0);
  2150. else if (fast_rexmit)
  2151. tcp_mark_head_lost(sk, 1, 1);
  2152. }
  2153. tcp_timeout_skbs(sk);
  2154. }
  2155. /* CWND moderation, preventing bursts due to too big ACKs
  2156. * in dubious situations.
  2157. */
  2158. static inline void tcp_moderate_cwnd(struct tcp_sock *tp)
  2159. {
  2160. tp->snd_cwnd = min(tp->snd_cwnd,
  2161. tcp_packets_in_flight(tp) + tcp_max_burst(tp));
  2162. tp->snd_cwnd_stamp = tcp_time_stamp;
  2163. }
  2164. /* Lower bound on congestion window is slow start threshold
  2165. * unless congestion avoidance choice decides to overide it.
  2166. */
  2167. static inline u32 tcp_cwnd_min(const struct sock *sk)
  2168. {
  2169. const struct tcp_congestion_ops *ca_ops = inet_csk(sk)->icsk_ca_ops;
  2170. return ca_ops->min_cwnd ? ca_ops->min_cwnd(sk) : tcp_sk(sk)->snd_ssthresh;
  2171. }
  2172. /* Decrease cwnd each second ack. */
  2173. static void tcp_cwnd_down(struct sock *sk, int flag)
  2174. {
  2175. struct tcp_sock *tp = tcp_sk(sk);
  2176. int decr = tp->snd_cwnd_cnt + 1;
  2177. if ((flag & (FLAG_ANY_PROGRESS | FLAG_DSACKING_ACK)) ||
  2178. (tcp_is_reno(tp) && !(flag & FLAG_NOT_DUP))) {
  2179. tp->snd_cwnd_cnt = decr & 1;
  2180. decr >>= 1;
  2181. if (decr && tp->snd_cwnd > tcp_cwnd_min(sk))
  2182. tp->snd_cwnd -= decr;
  2183. tp->snd_cwnd = min(tp->snd_cwnd, tcp_packets_in_flight(tp) + 1);
  2184. tp->snd_cwnd_stamp = tcp_time_stamp;
  2185. }
  2186. }
  2187. /* Nothing was retransmitted or returned timestamp is less
  2188. * than timestamp of the first retransmission.
  2189. */
  2190. static inline bool tcp_packet_delayed(const struct tcp_sock *tp)
  2191. {
  2192. return !tp->retrans_stamp ||
  2193. (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
  2194. before(tp->rx_opt.rcv_tsecr, tp->retrans_stamp));
  2195. }
  2196. /* Undo procedures. */
  2197. #if FASTRETRANS_DEBUG > 1
  2198. static void DBGUNDO(struct sock *sk, const char *msg)
  2199. {
  2200. struct tcp_sock *tp = tcp_sk(sk);
  2201. struct inet_sock *inet = inet_sk(sk);
  2202. if (sk->sk_family == AF_INET) {
  2203. pr_debug("Undo %s %pI4/%u c%u l%u ss%u/%u p%u\n",
  2204. msg,
  2205. &inet->inet_daddr, ntohs(inet->inet_dport),
  2206. tp->snd_cwnd, tcp_left_out(tp),
  2207. tp->snd_ssthresh, tp->prior_ssthresh,
  2208. tp->packets_out);
  2209. }
  2210. #if IS_ENABLED(CONFIG_IPV6)
  2211. else if (sk->sk_family == AF_INET6) {
  2212. struct ipv6_pinfo *np = inet6_sk(sk);
  2213. pr_debug("Undo %s %pI6/%u c%u l%u ss%u/%u p%u\n",
  2214. msg,
  2215. &np->daddr, ntohs(inet->inet_dport),
  2216. tp->snd_cwnd, tcp_left_out(tp),
  2217. tp->snd_ssthresh, tp->prior_ssthresh,
  2218. tp->packets_out);
  2219. }
  2220. #endif
  2221. }
  2222. #else
  2223. #define DBGUNDO(x...) do { } while (0)
  2224. #endif
  2225. static void tcp_undo_cwr(struct sock *sk, const bool undo_ssthresh)
  2226. {
  2227. struct tcp_sock *tp = tcp_sk(sk);
  2228. if (tp->prior_ssthresh) {
  2229. const struct inet_connection_sock *icsk = inet_csk(sk);
  2230. if (icsk->icsk_ca_ops->undo_cwnd)
  2231. tp->snd_cwnd = icsk->icsk_ca_ops->undo_cwnd(sk);
  2232. else
  2233. tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh << 1);
  2234. if (undo_ssthresh && tp->prior_ssthresh > tp->snd_ssthresh) {
  2235. tp->snd_ssthresh = tp->prior_ssthresh;
  2236. TCP_ECN_withdraw_cwr(tp);
  2237. }
  2238. } else {
  2239. tp->snd_cwnd = max(tp->snd_cwnd, tp->snd_ssthresh);
  2240. }
  2241. tp->snd_cwnd_stamp = tcp_time_stamp;
  2242. }
  2243. static inline bool tcp_may_undo(const struct tcp_sock *tp)
  2244. {
  2245. return tp->undo_marker && (!tp->undo_retrans || tcp_packet_delayed(tp));
  2246. }
  2247. /* People celebrate: "We love our President!" */
  2248. static bool tcp_try_undo_recovery(struct sock *sk)
  2249. {
  2250. struct tcp_sock *tp = tcp_sk(sk);
  2251. if (tcp_may_undo(tp)) {
  2252. int mib_idx;
  2253. /* Happy end! We did not retransmit anything
  2254. * or our original transmission succeeded.
  2255. */
  2256. DBGUNDO(sk, inet_csk(sk)->icsk_ca_state == TCP_CA_Loss ? "loss" : "retrans");
  2257. tcp_undo_cwr(sk, true);
  2258. if (inet_csk(sk)->icsk_ca_state == TCP_CA_Loss)
  2259. mib_idx = LINUX_MIB_TCPLOSSUNDO;
  2260. else
  2261. mib_idx = LINUX_MIB_TCPFULLUNDO;
  2262. NET_INC_STATS_BH(sock_net(sk), mib_idx);
  2263. tp->undo_marker = 0;
  2264. }
  2265. if (tp->snd_una == tp->high_seq && tcp_is_reno(tp)) {
  2266. /* Hold old state until something *above* high_seq
  2267. * is ACKed. For Reno it is MUST to prevent false
  2268. * fast retransmits (RFC2582). SACK TCP is safe. */
  2269. tcp_moderate_cwnd(tp);
  2270. return true;
  2271. }
  2272. tcp_set_ca_state(sk, TCP_CA_Open);
  2273. return false;
  2274. }
  2275. /* Try to undo cwnd reduction, because D-SACKs acked all retransmitted data */
  2276. static void tcp_try_undo_dsack(struct sock *sk)
  2277. {
  2278. struct tcp_sock *tp = tcp_sk(sk);
  2279. if (tp->undo_marker && !tp->undo_retrans) {
  2280. DBGUNDO(sk, "D-SACK");
  2281. tcp_undo_cwr(sk, true);
  2282. tp->undo_marker = 0;
  2283. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPDSACKUNDO);
  2284. }
  2285. }
  2286. /* We can clear retrans_stamp when there are no retransmissions in the
  2287. * window. It would seem that it is trivially available for us in
  2288. * tp->retrans_out, however, that kind of assumptions doesn't consider
  2289. * what will happen if errors occur when sending retransmission for the
  2290. * second time. ...It could the that such segment has only
  2291. * TCPCB_EVER_RETRANS set at the present time. It seems that checking
  2292. * the head skb is enough except for some reneging corner cases that
  2293. * are not worth the effort.
  2294. *
  2295. * Main reason for all this complexity is the fact that connection dying
  2296. * time now depends on the validity of the retrans_stamp, in particular,
  2297. * that successive retransmissions of a segment must not advance
  2298. * retrans_stamp under any conditions.
  2299. */
  2300. static bool tcp_any_retrans_done(const struct sock *sk)
  2301. {
  2302. const struct tcp_sock *tp = tcp_sk(sk);
  2303. struct sk_buff *skb;
  2304. if (tp->retrans_out)
  2305. return true;
  2306. skb = tcp_write_queue_head(sk);
  2307. if (unlikely(skb && TCP_SKB_CB(skb)->sacked & TCPCB_EVER_RETRANS))
  2308. return true;
  2309. return false;
  2310. }
  2311. /* Undo during fast recovery after partial ACK. */
  2312. static int tcp_try_undo_partial(struct sock *sk, int acked)
  2313. {
  2314. struct tcp_sock *tp = tcp_sk(sk);
  2315. /* Partial ACK arrived. Force Hoe's retransmit. */
  2316. int failed = tcp_is_reno(tp) || (tcp_fackets_out(tp) > tp->reordering);
  2317. if (tcp_may_undo(tp)) {
  2318. /* Plain luck! Hole if filled with delayed
  2319. * packet, rather than with a retransmit.
  2320. */
  2321. if (!tcp_any_retrans_done(sk))
  2322. tp->retrans_stamp = 0;
  2323. tcp_update_reordering(sk, tcp_fackets_out(tp) + acked, 1);
  2324. DBGUNDO(sk, "Hoe");
  2325. tcp_undo_cwr(sk, false);
  2326. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPARTIALUNDO);
  2327. /* So... Do not make Hoe's retransmit yet.
  2328. * If the first packet was delayed, the rest
  2329. * ones are most probably delayed as well.
  2330. */
  2331. failed = 0;
  2332. }
  2333. return failed;
  2334. }
  2335. /* Undo during loss recovery after partial ACK. */
  2336. static bool tcp_try_undo_loss(struct sock *sk)
  2337. {
  2338. struct tcp_sock *tp = tcp_sk(sk);
  2339. if (tcp_may_undo(tp)) {
  2340. struct sk_buff *skb;
  2341. tcp_for_write_queue(skb, sk) {
  2342. if (skb == tcp_send_head(sk))
  2343. break;
  2344. TCP_SKB_CB(skb)->sacked &= ~TCPCB_LOST;
  2345. }
  2346. tcp_clear_all_retrans_hints(tp);
  2347. DBGUNDO(sk, "partial loss");
  2348. tp->lost_out = 0;
  2349. tcp_undo_cwr(sk, true);
  2350. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPLOSSUNDO);
  2351. inet_csk(sk)->icsk_retransmits = 0;
  2352. tp->undo_marker = 0;
  2353. if (tcp_is_sack(tp))
  2354. tcp_set_ca_state(sk, TCP_CA_Open);
  2355. return true;
  2356. }
  2357. return false;
  2358. }
  2359. static inline void tcp_complete_cwr(struct sock *sk)
  2360. {
  2361. struct tcp_sock *tp = tcp_sk(sk);
  2362. /* Do not moderate cwnd if it's already undone in cwr or recovery. */
  2363. if (tp->undo_marker) {
  2364. if (inet_csk(sk)->icsk_ca_state == TCP_CA_CWR) {
  2365. tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh);
  2366. tp->snd_cwnd_stamp = tcp_time_stamp;
  2367. } else if (tp->snd_ssthresh < TCP_INFINITE_SSTHRESH) {
  2368. /* PRR algorithm. */
  2369. tp->snd_cwnd = tp->snd_ssthresh;
  2370. tp->snd_cwnd_stamp = tcp_time_stamp;
  2371. }
  2372. }
  2373. tcp_ca_event(sk, CA_EVENT_COMPLETE_CWR);
  2374. }
  2375. static void tcp_try_keep_open(struct sock *sk)
  2376. {
  2377. struct tcp_sock *tp = tcp_sk(sk);
  2378. int state = TCP_CA_Open;
  2379. if (tcp_left_out(tp) || tcp_any_retrans_done(sk))
  2380. state = TCP_CA_Disorder;
  2381. if (inet_csk(sk)->icsk_ca_state != state) {
  2382. tcp_set_ca_state(sk, state);
  2383. tp->high_seq = tp->snd_nxt;
  2384. }
  2385. }
  2386. static void tcp_try_to_open(struct sock *sk, int flag)
  2387. {
  2388. struct tcp_sock *tp = tcp_sk(sk);
  2389. tcp_verify_left_out(tp);
  2390. if (!tp->frto_counter && !tcp_any_retrans_done(sk))
  2391. tp->retrans_stamp = 0;
  2392. if (flag & FLAG_ECE)
  2393. tcp_enter_cwr(sk, 1);
  2394. if (inet_csk(sk)->icsk_ca_state != TCP_CA_CWR) {
  2395. tcp_try_keep_open(sk);
  2396. if (inet_csk(sk)->icsk_ca_state != TCP_CA_Open)
  2397. tcp_moderate_cwnd(tp);
  2398. } else {
  2399. tcp_cwnd_down(sk, flag);
  2400. }
  2401. }
  2402. static void tcp_mtup_probe_failed(struct sock *sk)
  2403. {
  2404. struct inet_connection_sock *icsk = inet_csk(sk);
  2405. icsk->icsk_mtup.search_high = icsk->icsk_mtup.probe_size - 1;
  2406. icsk->icsk_mtup.probe_size = 0;
  2407. }
  2408. static void tcp_mtup_probe_success(struct sock *sk)
  2409. {
  2410. struct tcp_sock *tp = tcp_sk(sk);
  2411. struct inet_connection_sock *icsk = inet_csk(sk);
  2412. /* FIXME: breaks with very large cwnd */
  2413. tp->prior_ssthresh = tcp_current_ssthresh(sk);
  2414. tp->snd_cwnd = tp->snd_cwnd *
  2415. tcp_mss_to_mtu(sk, tp->mss_cache) /
  2416. icsk->icsk_mtup.probe_size;
  2417. tp->snd_cwnd_cnt = 0;
  2418. tp->snd_cwnd_stamp = tcp_time_stamp;
  2419. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  2420. icsk->icsk_mtup.search_low = icsk->icsk_mtup.probe_size;
  2421. icsk->icsk_mtup.probe_size = 0;
  2422. tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  2423. }
  2424. /* Do a simple retransmit without using the backoff mechanisms in
  2425. * tcp_timer. This is used for path mtu discovery.
  2426. * The socket is already locked here.
  2427. */
  2428. void tcp_simple_retransmit(struct sock *sk)
  2429. {
  2430. const struct inet_connection_sock *icsk = inet_csk(sk);
  2431. struct tcp_sock *tp = tcp_sk(sk);
  2432. struct sk_buff *skb;
  2433. unsigned int mss = tcp_current_mss(sk);
  2434. u32 prior_lost = tp->lost_out;
  2435. tcp_for_write_queue(skb, sk) {
  2436. if (skb == tcp_send_head(sk))
  2437. break;
  2438. if (tcp_skb_seglen(skb) > mss &&
  2439. !(TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) {
  2440. if (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_RETRANS) {
  2441. TCP_SKB_CB(skb)->sacked &= ~TCPCB_SACKED_RETRANS;
  2442. tp->retrans_out -= tcp_skb_pcount(skb);
  2443. }
  2444. tcp_skb_mark_lost_uncond_verify(tp, skb);
  2445. }
  2446. }
  2447. tcp_clear_retrans_hints_partial(tp);
  2448. if (prior_lost == tp->lost_out)
  2449. return;
  2450. if (tcp_is_reno(tp))
  2451. tcp_limit_reno_sacked(tp);
  2452. tcp_verify_left_out(tp);
  2453. /* Don't muck with the congestion window here.
  2454. * Reason is that we do not increase amount of _data_
  2455. * in network, but units changed and effective
  2456. * cwnd/ssthresh really reduced now.
  2457. */
  2458. if (icsk->icsk_ca_state != TCP_CA_Loss) {
  2459. tp->high_seq = tp->snd_nxt;
  2460. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  2461. tp->prior_ssthresh = 0;
  2462. tp->undo_marker = 0;
  2463. tcp_set_ca_state(sk, TCP_CA_Loss);
  2464. }
  2465. tcp_xmit_retransmit_queue(sk);
  2466. }
  2467. EXPORT_SYMBOL(tcp_simple_retransmit);
  2468. /* This function implements the PRR algorithm, specifcally the PRR-SSRB
  2469. * (proportional rate reduction with slow start reduction bound) as described in
  2470. * http://www.ietf.org/id/draft-mathis-tcpm-proportional-rate-reduction-01.txt.
  2471. * It computes the number of packets to send (sndcnt) based on packets newly
  2472. * delivered:
  2473. * 1) If the packets in flight is larger than ssthresh, PRR spreads the
  2474. * cwnd reductions across a full RTT.
  2475. * 2) If packets in flight is lower than ssthresh (such as due to excess
  2476. * losses and/or application stalls), do not perform any further cwnd
  2477. * reductions, but instead slow start up to ssthresh.
  2478. */
  2479. static void tcp_update_cwnd_in_recovery(struct sock *sk, int newly_acked_sacked,
  2480. int fast_rexmit, int flag)
  2481. {
  2482. struct tcp_sock *tp = tcp_sk(sk);
  2483. int sndcnt = 0;
  2484. int delta = tp->snd_ssthresh - tcp_packets_in_flight(tp);
  2485. if (tcp_packets_in_flight(tp) > tp->snd_ssthresh) {
  2486. u64 dividend = (u64)tp->snd_ssthresh * tp->prr_delivered +
  2487. tp->prior_cwnd - 1;
  2488. sndcnt = div_u64(dividend, tp->prior_cwnd) - tp->prr_out;
  2489. } else {
  2490. sndcnt = min_t(int, delta,
  2491. max_t(int, tp->prr_delivered - tp->prr_out,
  2492. newly_acked_sacked) + 1);
  2493. }
  2494. sndcnt = max(sndcnt, (fast_rexmit ? 1 : 0));
  2495. tp->snd_cwnd = tcp_packets_in_flight(tp) + sndcnt;
  2496. }
  2497. static void tcp_enter_recovery(struct sock *sk, bool ece_ack)
  2498. {
  2499. struct tcp_sock *tp = tcp_sk(sk);
  2500. int mib_idx;
  2501. if (tcp_is_reno(tp))
  2502. mib_idx = LINUX_MIB_TCPRENORECOVERY;
  2503. else
  2504. mib_idx = LINUX_MIB_TCPSACKRECOVERY;
  2505. NET_INC_STATS_BH(sock_net(sk), mib_idx);
  2506. tp->high_seq = tp->snd_nxt;
  2507. tp->prior_ssthresh = 0;
  2508. tp->undo_marker = tp->snd_una;
  2509. tp->undo_retrans = tp->retrans_out;
  2510. if (inet_csk(sk)->icsk_ca_state < TCP_CA_CWR) {
  2511. if (!ece_ack)
  2512. tp->prior_ssthresh = tcp_current_ssthresh(sk);
  2513. tp->snd_ssthresh = inet_csk(sk)->icsk_ca_ops->ssthresh(sk);
  2514. TCP_ECN_queue_cwr(tp);
  2515. }
  2516. tp->bytes_acked = 0;
  2517. tp->snd_cwnd_cnt = 0;
  2518. tp->prior_cwnd = tp->snd_cwnd;
  2519. tp->prr_delivered = 0;
  2520. tp->prr_out = 0;
  2521. tcp_set_ca_state(sk, TCP_CA_Recovery);
  2522. }
  2523. /* Process an event, which can update packets-in-flight not trivially.
  2524. * Main goal of this function is to calculate new estimate for left_out,
  2525. * taking into account both packets sitting in receiver's buffer and
  2526. * packets lost by network.
  2527. *
  2528. * Besides that it does CWND reduction, when packet loss is detected
  2529. * and changes state of machine.
  2530. *
  2531. * It does _not_ decide what to send, it is made in function
  2532. * tcp_xmit_retransmit_queue().
  2533. */
  2534. static void tcp_fastretrans_alert(struct sock *sk, int pkts_acked,
  2535. int newly_acked_sacked, bool is_dupack,
  2536. int flag)
  2537. {
  2538. struct inet_connection_sock *icsk = inet_csk(sk);
  2539. struct tcp_sock *tp = tcp_sk(sk);
  2540. int do_lost = is_dupack || ((flag & FLAG_DATA_SACKED) &&
  2541. (tcp_fackets_out(tp) > tp->reordering));
  2542. int fast_rexmit = 0;
  2543. if (WARN_ON(!tp->packets_out && tp->sacked_out))
  2544. tp->sacked_out = 0;
  2545. if (WARN_ON(!tp->sacked_out && tp->fackets_out))
  2546. tp->fackets_out = 0;
  2547. /* Now state machine starts.
  2548. * A. ECE, hence prohibit cwnd undoing, the reduction is required. */
  2549. if (flag & FLAG_ECE)
  2550. tp->prior_ssthresh = 0;
  2551. /* B. In all the states check for reneging SACKs. */
  2552. if (tcp_check_sack_reneging(sk, flag))
  2553. return;
  2554. /* C. Check consistency of the current state. */
  2555. tcp_verify_left_out(tp);
  2556. /* D. Check state exit conditions. State can be terminated
  2557. * when high_seq is ACKed. */
  2558. if (icsk->icsk_ca_state == TCP_CA_Open) {
  2559. WARN_ON(tp->retrans_out != 0);
  2560. tp->retrans_stamp = 0;
  2561. } else if (!before(tp->snd_una, tp->high_seq)) {
  2562. switch (icsk->icsk_ca_state) {
  2563. case TCP_CA_Loss:
  2564. icsk->icsk_retransmits = 0;
  2565. if (tcp_try_undo_recovery(sk))
  2566. return;
  2567. break;
  2568. case TCP_CA_CWR:
  2569. /* CWR is to be held something *above* high_seq
  2570. * is ACKed for CWR bit to reach receiver. */
  2571. if (tp->snd_una != tp->high_seq) {
  2572. tcp_complete_cwr(sk);
  2573. tcp_set_ca_state(sk, TCP_CA_Open);
  2574. }
  2575. break;
  2576. case TCP_CA_Recovery:
  2577. if (tcp_is_reno(tp))
  2578. tcp_reset_reno_sack(tp);
  2579. if (tcp_try_undo_recovery(sk))
  2580. return;
  2581. tcp_complete_cwr(sk);
  2582. break;
  2583. }
  2584. }
  2585. /* E. Process state. */
  2586. switch (icsk->icsk_ca_state) {
  2587. case TCP_CA_Recovery:
  2588. if (!(flag & FLAG_SND_UNA_ADVANCED)) {
  2589. if (tcp_is_reno(tp) && is_dupack)
  2590. tcp_add_reno_sack(sk);
  2591. } else
  2592. do_lost = tcp_try_undo_partial(sk, pkts_acked);
  2593. break;
  2594. case TCP_CA_Loss:
  2595. if (flag & FLAG_DATA_ACKED)
  2596. icsk->icsk_retransmits = 0;
  2597. if (tcp_is_reno(tp) && flag & FLAG_SND_UNA_ADVANCED)
  2598. tcp_reset_reno_sack(tp);
  2599. if (!tcp_try_undo_loss(sk)) {
  2600. tcp_moderate_cwnd(tp);
  2601. tcp_xmit_retransmit_queue(sk);
  2602. return;
  2603. }
  2604. if (icsk->icsk_ca_state != TCP_CA_Open)
  2605. return;
  2606. /* Loss is undone; fall through to processing in Open state. */
  2607. default:
  2608. if (tcp_is_reno(tp)) {
  2609. if (flag & FLAG_SND_UNA_ADVANCED)
  2610. tcp_reset_reno_sack(tp);
  2611. if (is_dupack)
  2612. tcp_add_reno_sack(sk);
  2613. }
  2614. if (icsk->icsk_ca_state <= TCP_CA_Disorder)
  2615. tcp_try_undo_dsack(sk);
  2616. if (!tcp_time_to_recover(sk, flag)) {
  2617. tcp_try_to_open(sk, flag);
  2618. return;
  2619. }
  2620. /* MTU probe failure: don't reduce cwnd */
  2621. if (icsk->icsk_ca_state < TCP_CA_CWR &&
  2622. icsk->icsk_mtup.probe_size &&
  2623. tp->snd_una == tp->mtu_probe.probe_seq_start) {
  2624. tcp_mtup_probe_failed(sk);
  2625. /* Restores the reduction we did in tcp_mtup_probe() */
  2626. tp->snd_cwnd++;
  2627. tcp_simple_retransmit(sk);
  2628. return;
  2629. }
  2630. /* Otherwise enter Recovery state */
  2631. tcp_enter_recovery(sk, (flag & FLAG_ECE));
  2632. fast_rexmit = 1;
  2633. }
  2634. if (do_lost || (tcp_is_fack(tp) && tcp_head_timedout(sk)))
  2635. tcp_update_scoreboard(sk, fast_rexmit);
  2636. tp->prr_delivered += newly_acked_sacked;
  2637. tcp_update_cwnd_in_recovery(sk, newly_acked_sacked, fast_rexmit, flag);
  2638. tcp_xmit_retransmit_queue(sk);
  2639. }
  2640. void tcp_valid_rtt_meas(struct sock *sk, u32 seq_rtt)
  2641. {
  2642. tcp_rtt_estimator(sk, seq_rtt);
  2643. tcp_set_rto(sk);
  2644. inet_csk(sk)->icsk_backoff = 0;
  2645. }
  2646. EXPORT_SYMBOL(tcp_valid_rtt_meas);
  2647. /* Read draft-ietf-tcplw-high-performance before mucking
  2648. * with this code. (Supersedes RFC1323)
  2649. */
  2650. static void tcp_ack_saw_tstamp(struct sock *sk, int flag)
  2651. {
  2652. /* RTTM Rule: A TSecr value received in a segment is used to
  2653. * update the averaged RTT measurement only if the segment
  2654. * acknowledges some new data, i.e., only if it advances the
  2655. * left edge of the send window.
  2656. *
  2657. * See draft-ietf-tcplw-high-performance-00, section 3.3.
  2658. * 1998/04/10 Andrey V. Savochkin <saw@msu.ru>
  2659. *
  2660. * Changed: reset backoff as soon as we see the first valid sample.
  2661. * If we do not, we get strongly overestimated rto. With timestamps
  2662. * samples are accepted even from very old segments: f.e., when rtt=1
  2663. * increases to 8, we retransmit 5 times and after 8 seconds delayed
  2664. * answer arrives rto becomes 120 seconds! If at least one of segments
  2665. * in window is lost... Voila. --ANK (010210)
  2666. */
  2667. struct tcp_sock *tp = tcp_sk(sk);
  2668. tcp_valid_rtt_meas(sk, tcp_time_stamp - tp->rx_opt.rcv_tsecr);
  2669. }
  2670. static void tcp_ack_no_tstamp(struct sock *sk, u32 seq_rtt, int flag)
  2671. {
  2672. /* We don't have a timestamp. Can only use
  2673. * packets that are not retransmitted to determine
  2674. * rtt estimates. Also, we must not reset the
  2675. * backoff for rto until we get a non-retransmitted
  2676. * packet. This allows us to deal with a situation
  2677. * where the network delay has increased suddenly.
  2678. * I.e. Karn's algorithm. (SIGCOMM '87, p5.)
  2679. */
  2680. if (flag & FLAG_RETRANS_DATA_ACKED)
  2681. return;
  2682. tcp_valid_rtt_meas(sk, seq_rtt);
  2683. }
  2684. static inline void tcp_ack_update_rtt(struct sock *sk, const int flag,
  2685. const s32 seq_rtt)
  2686. {
  2687. const struct tcp_sock *tp = tcp_sk(sk);
  2688. /* Note that peer MAY send zero echo. In this case it is ignored. (rfc1323) */
  2689. if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr)
  2690. tcp_ack_saw_tstamp(sk, flag);
  2691. else if (seq_rtt >= 0)
  2692. tcp_ack_no_tstamp(sk, seq_rtt, flag);
  2693. }
  2694. static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
  2695. {
  2696. const struct inet_connection_sock *icsk = inet_csk(sk);
  2697. icsk->icsk_ca_ops->cong_avoid(sk, ack, in_flight);
  2698. tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;
  2699. }
  2700. /* Restart timer after forward progress on connection.
  2701. * RFC2988 recommends to restart timer to now+rto.
  2702. */
  2703. void tcp_rearm_rto(struct sock *sk)
  2704. {
  2705. struct tcp_sock *tp = tcp_sk(sk);
  2706. if (!tp->packets_out) {
  2707. inet_csk_clear_xmit_timer(sk, ICSK_TIME_RETRANS);
  2708. } else {
  2709. u32 rto = inet_csk(sk)->icsk_rto;
  2710. /* Offset the time elapsed after installing regular RTO */
  2711. if (tp->early_retrans_delayed) {
  2712. struct sk_buff *skb = tcp_write_queue_head(sk);
  2713. const u32 rto_time_stamp = TCP_SKB_CB(skb)->when + rto;
  2714. s32 delta = (s32)(rto_time_stamp - tcp_time_stamp);
  2715. /* delta may not be positive if the socket is locked
  2716. * when the delayed ER timer fires and is rescheduled.
  2717. */
  2718. if (delta > 0)
  2719. rto = delta;
  2720. }
  2721. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, rto,
  2722. TCP_RTO_MAX);
  2723. }
  2724. tp->early_retrans_delayed = 0;
  2725. }
  2726. /* This function is called when the delayed ER timer fires. TCP enters
  2727. * fast recovery and performs fast-retransmit.
  2728. */
  2729. void tcp_resume_early_retransmit(struct sock *sk)
  2730. {
  2731. struct tcp_sock *tp = tcp_sk(sk);
  2732. tcp_rearm_rto(sk);
  2733. /* Stop if ER is disabled after the delayed ER timer is scheduled */
  2734. if (!tp->do_early_retrans)
  2735. return;
  2736. tcp_enter_recovery(sk, false);
  2737. tcp_update_scoreboard(sk, 1);
  2738. tcp_xmit_retransmit_queue(sk);
  2739. }
  2740. /* If we get here, the whole TSO packet has not been acked. */
  2741. static u32 tcp_tso_acked(struct sock *sk, struct sk_buff *skb)
  2742. {
  2743. struct tcp_sock *tp = tcp_sk(sk);
  2744. u32 packets_acked;
  2745. BUG_ON(!after(TCP_SKB_CB(skb)->end_seq, tp->snd_una));
  2746. packets_acked = tcp_skb_pcount(skb);
  2747. if (tcp_trim_head(sk, skb, tp->snd_una - TCP_SKB_CB(skb)->seq))
  2748. return 0;
  2749. packets_acked -= tcp_skb_pcount(skb);
  2750. if (packets_acked) {
  2751. BUG_ON(tcp_skb_pcount(skb) == 0);
  2752. BUG_ON(!before(TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq));
  2753. }
  2754. return packets_acked;
  2755. }
  2756. /* Remove acknowledged frames from the retransmission queue. If our packet
  2757. * is before the ack sequence we can discard it as it's confirmed to have
  2758. * arrived at the other end.
  2759. */
  2760. static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
  2761. u32 prior_snd_una)
  2762. {
  2763. struct tcp_sock *tp = tcp_sk(sk);
  2764. const struct inet_connection_sock *icsk = inet_csk(sk);
  2765. struct sk_buff *skb;
  2766. u32 now = tcp_time_stamp;
  2767. int fully_acked = true;
  2768. int flag = 0;
  2769. u32 pkts_acked = 0;
  2770. u32 reord = tp->packets_out;
  2771. u32 prior_sacked = tp->sacked_out;
  2772. s32 seq_rtt = -1;
  2773. s32 ca_seq_rtt = -1;
  2774. ktime_t last_ackt = net_invalid_timestamp();
  2775. while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {
  2776. struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
  2777. u32 acked_pcount;
  2778. u8 sacked = scb->sacked;
  2779. /* Determine how many packets and what bytes were acked, tso and else */
  2780. if (after(scb->end_seq, tp->snd_una)) {
  2781. if (tcp_skb_pcount(skb) == 1 ||
  2782. !after(tp->snd_una, scb->seq))
  2783. break;
  2784. acked_pcount = tcp_tso_acked(sk, skb);
  2785. if (!acked_pcount)
  2786. break;
  2787. fully_acked = false;
  2788. } else {
  2789. acked_pcount = tcp_skb_pcount(skb);
  2790. }
  2791. if (sacked & TCPCB_RETRANS) {
  2792. if (sacked & TCPCB_SACKED_RETRANS)
  2793. tp->retrans_out -= acked_pcount;
  2794. flag |= FLAG_RETRANS_DATA_ACKED;
  2795. ca_seq_rtt = -1;
  2796. seq_rtt = -1;
  2797. if ((flag & FLAG_DATA_ACKED) || (acked_pcount > 1))
  2798. flag |= FLAG_NONHEAD_RETRANS_ACKED;
  2799. } else {
  2800. ca_seq_rtt = now - scb->when;
  2801. last_ackt = skb->tstamp;
  2802. if (seq_rtt < 0) {
  2803. seq_rtt = ca_seq_rtt;
  2804. }
  2805. if (!(sacked & TCPCB_SACKED_ACKED))
  2806. reord = min(pkts_acked, reord);
  2807. }
  2808. if (sacked & TCPCB_SACKED_ACKED)
  2809. tp->sacked_out -= acked_pcount;
  2810. if (sacked & TCPCB_LOST)
  2811. tp->lost_out -= acked_pcount;
  2812. tp->packets_out -= acked_pcount;
  2813. pkts_acked += acked_pcount;
  2814. /* Initial outgoing SYN's get put onto the write_queue
  2815. * just like anything else we transmit. It is not
  2816. * true data, and if we misinform our callers that
  2817. * this ACK acks real data, we will erroneously exit
  2818. * connection startup slow start one packet too
  2819. * quickly. This is severely frowned upon behavior.
  2820. */
  2821. if (!(scb->tcp_flags & TCPHDR_SYN)) {
  2822. flag |= FLAG_DATA_ACKED;
  2823. } else {
  2824. flag |= FLAG_SYN_ACKED;
  2825. tp->retrans_stamp = 0;
  2826. }
  2827. if (!fully_acked)
  2828. break;
  2829. tcp_unlink_write_queue(skb, sk);
  2830. sk_wmem_free_skb(sk, skb);
  2831. tp->scoreboard_skb_hint = NULL;
  2832. if (skb == tp->retransmit_skb_hint)
  2833. tp->retransmit_skb_hint = NULL;
  2834. if (skb == tp->lost_skb_hint)
  2835. tp->lost_skb_hint = NULL;
  2836. }
  2837. if (likely(between(tp->snd_up, prior_snd_una, tp->snd_una)))
  2838. tp->snd_up = tp->snd_una;
  2839. if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
  2840. flag |= FLAG_SACK_RENEGING;
  2841. if (flag & FLAG_ACKED) {
  2842. const struct tcp_congestion_ops *ca_ops
  2843. = inet_csk(sk)->icsk_ca_ops;
  2844. if (unlikely(icsk->icsk_mtup.probe_size &&
  2845. !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
  2846. tcp_mtup_probe_success(sk);
  2847. }
  2848. tcp_ack_update_rtt(sk, flag, seq_rtt);
  2849. tcp_rearm_rto(sk);
  2850. if (tcp_is_reno(tp)) {
  2851. tcp_remove_reno_sacks(sk, pkts_acked);
  2852. } else {
  2853. int delta;
  2854. /* Non-retransmitted hole got filled? That's reordering */
  2855. if (reord < prior_fackets)
  2856. tcp_update_reordering(sk, tp->fackets_out - reord, 0);
  2857. delta = tcp_is_fack(tp) ? pkts_acked :
  2858. prior_sacked - tp->sacked_out;
  2859. tp->lost_cnt_hint -= min(tp->lost_cnt_hint, delta);
  2860. }
  2861. tp->fackets_out -= min(pkts_acked, tp->fackets_out);
  2862. if (ca_ops->pkts_acked) {
  2863. s32 rtt_us = -1;
  2864. /* Is the ACK triggering packet unambiguous? */
  2865. if (!(flag & FLAG_RETRANS_DATA_ACKED)) {
  2866. /* High resolution needed and available? */
  2867. if (ca_ops->flags & TCP_CONG_RTT_STAMP &&
  2868. !ktime_equal(last_ackt,
  2869. net_invalid_timestamp()))
  2870. rtt_us = ktime_us_delta(ktime_get_real(),
  2871. last_ackt);
  2872. else if (ca_seq_rtt >= 0)
  2873. rtt_us = jiffies_to_usecs(ca_seq_rtt);
  2874. }
  2875. ca_ops->pkts_acked(sk, pkts_acked, rtt_us);
  2876. }
  2877. }
  2878. #if FASTRETRANS_DEBUG > 0
  2879. WARN_ON((int)tp->sacked_out < 0);
  2880. WARN_ON((int)tp->lost_out < 0);
  2881. WARN_ON((int)tp->retrans_out < 0);
  2882. if (!tp->packets_out && tcp_is_sack(tp)) {
  2883. icsk = inet_csk(sk);
  2884. if (tp->lost_out) {
  2885. pr_debug("Leak l=%u %d\n",
  2886. tp->lost_out, icsk->icsk_ca_state);
  2887. tp->lost_out = 0;
  2888. }
  2889. if (tp->sacked_out) {
  2890. pr_debug("Leak s=%u %d\n",
  2891. tp->sacked_out, icsk->icsk_ca_state);
  2892. tp->sacked_out = 0;
  2893. }
  2894. if (tp->retrans_out) {
  2895. pr_debug("Leak r=%u %d\n",
  2896. tp->retrans_out, icsk->icsk_ca_state);
  2897. tp->retrans_out = 0;
  2898. }
  2899. }
  2900. #endif
  2901. return flag;
  2902. }
  2903. static void tcp_ack_probe(struct sock *sk)
  2904. {
  2905. const struct tcp_sock *tp = tcp_sk(sk);
  2906. struct inet_connection_sock *icsk = inet_csk(sk);
  2907. /* Was it a usable window open? */
  2908. if (!after(TCP_SKB_CB(tcp_send_head(sk))->end_seq, tcp_wnd_end(tp))) {
  2909. icsk->icsk_backoff = 0;
  2910. inet_csk_clear_xmit_timer(sk, ICSK_TIME_PROBE0);
  2911. /* Socket must be waked up by subsequent tcp_data_snd_check().
  2912. * This function is not for random using!
  2913. */
  2914. } else {
  2915. inet_csk_reset_xmit_timer(sk, ICSK_TIME_PROBE0,
  2916. min(icsk->icsk_rto << icsk->icsk_backoff, TCP_RTO_MAX),
  2917. TCP_RTO_MAX);
  2918. }
  2919. }
  2920. static inline bool tcp_ack_is_dubious(const struct sock *sk, const int flag)
  2921. {
  2922. return !(flag & FLAG_NOT_DUP) || (flag & FLAG_CA_ALERT) ||
  2923. inet_csk(sk)->icsk_ca_state != TCP_CA_Open;
  2924. }
  2925. static inline bool tcp_may_raise_cwnd(const struct sock *sk, const int flag)
  2926. {
  2927. const struct tcp_sock *tp = tcp_sk(sk);
  2928. return (!(flag & FLAG_ECE) || tp->snd_cwnd < tp->snd_ssthresh) &&
  2929. !((1 << inet_csk(sk)->icsk_ca_state) & (TCPF_CA_Recovery | TCPF_CA_CWR));
  2930. }
  2931. /* Check that window update is acceptable.
  2932. * The function assumes that snd_una<=ack<=snd_next.
  2933. */
  2934. static inline bool tcp_may_update_window(const struct tcp_sock *tp,
  2935. const u32 ack, const u32 ack_seq,
  2936. const u32 nwin)
  2937. {
  2938. return after(ack, tp->snd_una) ||
  2939. after(ack_seq, tp->snd_wl1) ||
  2940. (ack_seq == tp->snd_wl1 && nwin > tp->snd_wnd);
  2941. }
  2942. /* Update our send window.
  2943. *
  2944. * Window update algorithm, described in RFC793/RFC1122 (used in linux-2.2
  2945. * and in FreeBSD. NetBSD's one is even worse.) is wrong.
  2946. */
  2947. static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32 ack,
  2948. u32 ack_seq)
  2949. {
  2950. struct tcp_sock *tp = tcp_sk(sk);
  2951. int flag = 0;
  2952. u32 nwin = ntohs(tcp_hdr(skb)->window);
  2953. if (likely(!tcp_hdr(skb)->syn))
  2954. nwin <<= tp->rx_opt.snd_wscale;
  2955. if (tcp_may_update_window(tp, ack, ack_seq, nwin)) {
  2956. flag |= FLAG_WIN_UPDATE;
  2957. tcp_update_wl(tp, ack_seq);
  2958. if (tp->snd_wnd != nwin) {
  2959. tp->snd_wnd = nwin;
  2960. /* Note, it is the only place, where
  2961. * fast path is recovered for sending TCP.
  2962. */
  2963. tp->pred_flags = 0;
  2964. tcp_fast_path_check(sk);
  2965. if (nwin > tp->max_window) {
  2966. tp->max_window = nwin;
  2967. tcp_sync_mss(sk, inet_csk(sk)->icsk_pmtu_cookie);
  2968. }
  2969. }
  2970. }
  2971. tp->snd_una = ack;
  2972. return flag;
  2973. }
  2974. /* A very conservative spurious RTO response algorithm: reduce cwnd and
  2975. * continue in congestion avoidance.
  2976. */
  2977. static void tcp_conservative_spur_to_response(struct tcp_sock *tp)
  2978. {
  2979. tp->snd_cwnd = min(tp->snd_cwnd, tp->snd_ssthresh);
  2980. tp->snd_cwnd_cnt = 0;
  2981. tp->bytes_acked = 0;
  2982. TCP_ECN_queue_cwr(tp);
  2983. tcp_moderate_cwnd(tp);
  2984. }
  2985. /* A conservative spurious RTO response algorithm: reduce cwnd using
  2986. * rate halving and continue in congestion avoidance.
  2987. */
  2988. static void tcp_ratehalving_spur_to_response(struct sock *sk)
  2989. {
  2990. tcp_enter_cwr(sk, 0);
  2991. }
  2992. static void tcp_undo_spur_to_response(struct sock *sk, int flag)
  2993. {
  2994. if (flag & FLAG_ECE)
  2995. tcp_ratehalving_spur_to_response(sk);
  2996. else
  2997. tcp_undo_cwr(sk, true);
  2998. }
  2999. /* F-RTO spurious RTO detection algorithm (RFC4138)
  3000. *
  3001. * F-RTO affects during two new ACKs following RTO (well, almost, see inline
  3002. * comments). State (ACK number) is kept in frto_counter. When ACK advances
  3003. * window (but not to or beyond highest sequence sent before RTO):
  3004. * On First ACK, send two new segments out.
  3005. * On Second ACK, RTO was likely spurious. Do spurious response (response
  3006. * algorithm is not part of the F-RTO detection algorithm
  3007. * given in RFC4138 but can be selected separately).
  3008. * Otherwise (basically on duplicate ACK), RTO was (likely) caused by a loss
  3009. * and TCP falls back to conventional RTO recovery. F-RTO allows overriding
  3010. * of Nagle, this is done using frto_counter states 2 and 3, when a new data
  3011. * segment of any size sent during F-RTO, state 2 is upgraded to 3.
  3012. *
  3013. * Rationale: if the RTO was spurious, new ACKs should arrive from the
  3014. * original window even after we transmit two new data segments.
  3015. *
  3016. * SACK version:
  3017. * on first step, wait until first cumulative ACK arrives, then move to
  3018. * the second step. In second step, the next ACK decides.
  3019. *
  3020. * F-RTO is implemented (mainly) in four functions:
  3021. * - tcp_use_frto() is used to determine if TCP is can use F-RTO
  3022. * - tcp_enter_frto() prepares TCP state on RTO if F-RTO is used, it is
  3023. * called when tcp_use_frto() showed green light
  3024. * - tcp_process_frto() handles incoming ACKs during F-RTO algorithm
  3025. * - tcp_enter_frto_loss() is called if there is not enough evidence
  3026. * to prove that the RTO is indeed spurious. It transfers the control
  3027. * from F-RTO to the conventional RTO recovery
  3028. */
  3029. static bool tcp_process_frto(struct sock *sk, int flag)
  3030. {
  3031. struct tcp_sock *tp = tcp_sk(sk);
  3032. tcp_verify_left_out(tp);
  3033. /* Duplicate the behavior from Loss state (fastretrans_alert) */
  3034. if (flag & FLAG_DATA_ACKED)
  3035. inet_csk(sk)->icsk_retransmits = 0;
  3036. if ((flag & FLAG_NONHEAD_RETRANS_ACKED) ||
  3037. ((tp->frto_counter >= 2) && (flag & FLAG_RETRANS_DATA_ACKED)))
  3038. tp->undo_marker = 0;
  3039. if (!before(tp->snd_una, tp->frto_highmark)) {
  3040. tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 2 : 3), flag);
  3041. return true;
  3042. }
  3043. if (!tcp_is_sackfrto(tp)) {
  3044. /* RFC4138 shortcoming in step 2; should also have case c):
  3045. * ACK isn't duplicate nor advances window, e.g., opposite dir
  3046. * data, winupdate
  3047. */
  3048. if (!(flag & FLAG_ANY_PROGRESS) && (flag & FLAG_NOT_DUP))
  3049. return true;
  3050. if (!(flag & FLAG_DATA_ACKED)) {
  3051. tcp_enter_frto_loss(sk, (tp->frto_counter == 1 ? 0 : 3),
  3052. flag);
  3053. return true;
  3054. }
  3055. } else {
  3056. if (!(flag & FLAG_DATA_ACKED) && (tp->frto_counter == 1)) {
  3057. /* Prevent sending of new data. */
  3058. tp->snd_cwnd = min(tp->snd_cwnd,
  3059. tcp_packets_in_flight(tp));
  3060. return true;
  3061. }
  3062. if ((tp->frto_counter >= 2) &&
  3063. (!(flag & FLAG_FORWARD_PROGRESS) ||
  3064. ((flag & FLAG_DATA_SACKED) &&
  3065. !(flag & FLAG_ONLY_ORIG_SACKED)))) {
  3066. /* RFC4138 shortcoming (see comment above) */
  3067. if (!(flag & FLAG_FORWARD_PROGRESS) &&
  3068. (flag & FLAG_NOT_DUP))
  3069. return true;
  3070. tcp_enter_frto_loss(sk, 3, flag);
  3071. return true;
  3072. }
  3073. }
  3074. if (tp->frto_counter == 1) {
  3075. /* tcp_may_send_now needs to see updated state */
  3076. tp->snd_cwnd = tcp_packets_in_flight(tp) + 2;
  3077. tp->frto_counter = 2;
  3078. if (!tcp_may_send_now(sk))
  3079. tcp_enter_frto_loss(sk, 2, flag);
  3080. return true;
  3081. } else {
  3082. switch (sysctl_tcp_frto_response) {
  3083. case 2:
  3084. tcp_undo_spur_to_response(sk, flag);
  3085. break;
  3086. case 1:
  3087. tcp_conservative_spur_to_response(tp);
  3088. break;
  3089. default:
  3090. tcp_ratehalving_spur_to_response(sk);
  3091. break;
  3092. }
  3093. tp->frto_counter = 0;
  3094. tp->undo_marker = 0;
  3095. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSPURIOUSRTOS);
  3096. }
  3097. return false;
  3098. }
  3099. /* This routine deals with incoming acks, but not outgoing ones. */
  3100. static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
  3101. {
  3102. struct inet_connection_sock *icsk = inet_csk(sk);
  3103. struct tcp_sock *tp = tcp_sk(sk);
  3104. u32 prior_snd_una = tp->snd_una;
  3105. u32 ack_seq = TCP_SKB_CB(skb)->seq;
  3106. u32 ack = TCP_SKB_CB(skb)->ack_seq;
  3107. bool is_dupack = false;
  3108. u32 prior_in_flight;
  3109. u32 prior_fackets;
  3110. int prior_packets;
  3111. int prior_sacked = tp->sacked_out;
  3112. int pkts_acked = 0;
  3113. int newly_acked_sacked = 0;
  3114. bool frto_cwnd = false;
  3115. /* If the ack is older than previous acks
  3116. * then we can probably ignore it.
  3117. */
  3118. if (before(ack, prior_snd_una))
  3119. goto old_ack;
  3120. /* If the ack includes data we haven't sent yet, discard
  3121. * this segment (RFC793 Section 3.9).
  3122. */
  3123. if (after(ack, tp->snd_nxt))
  3124. goto invalid_ack;
  3125. if (tp->early_retrans_delayed)
  3126. tcp_rearm_rto(sk);
  3127. if (after(ack, prior_snd_una))
  3128. flag |= FLAG_SND_UNA_ADVANCED;
  3129. if (sysctl_tcp_abc) {
  3130. if (icsk->icsk_ca_state < TCP_CA_CWR)
  3131. tp->bytes_acked += ack - prior_snd_una;
  3132. else if (icsk->icsk_ca_state == TCP_CA_Loss)
  3133. /* we assume just one segment left network */
  3134. tp->bytes_acked += min(ack - prior_snd_una,
  3135. tp->mss_cache);
  3136. }
  3137. prior_fackets = tp->fackets_out;
  3138. prior_in_flight = tcp_packets_in_flight(tp);
  3139. if (!(flag & FLAG_SLOWPATH) && after(ack, prior_snd_una)) {
  3140. /* Window is constant, pure forward advance.
  3141. * No more checks are required.
  3142. * Note, we use the fact that SND.UNA>=SND.WL2.
  3143. */
  3144. tcp_update_wl(tp, ack_seq);
  3145. tp->snd_una = ack;
  3146. flag |= FLAG_WIN_UPDATE;
  3147. tcp_ca_event(sk, CA_EVENT_FAST_ACK);
  3148. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPACKS);
  3149. } else {
  3150. if (ack_seq != TCP_SKB_CB(skb)->end_seq)
  3151. flag |= FLAG_DATA;
  3152. else
  3153. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPPUREACKS);
  3154. flag |= tcp_ack_update_window(sk, skb, ack, ack_seq);
  3155. if (TCP_SKB_CB(skb)->sacked)
  3156. flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una);
  3157. if (TCP_ECN_rcv_ecn_echo(tp, tcp_hdr(skb)))
  3158. flag |= FLAG_ECE;
  3159. tcp_ca_event(sk, CA_EVENT_SLOW_ACK);
  3160. }
  3161. /* We passed data and got it acked, remove any soft error
  3162. * log. Something worked...
  3163. */
  3164. sk->sk_err_soft = 0;
  3165. icsk->icsk_probes_out = 0;
  3166. tp->rcv_tstamp = tcp_time_stamp;
  3167. prior_packets = tp->packets_out;
  3168. if (!prior_packets)
  3169. goto no_queue;
  3170. /* See if we can take anything off of the retransmit queue. */
  3171. flag |= tcp_clean_rtx_queue(sk, prior_fackets, prior_snd_una);
  3172. pkts_acked = prior_packets - tp->packets_out;
  3173. newly_acked_sacked = (prior_packets - prior_sacked) -
  3174. (tp->packets_out - tp->sacked_out);
  3175. if (tp->frto_counter)
  3176. frto_cwnd = tcp_process_frto(sk, flag);
  3177. /* Guarantee sacktag reordering detection against wrap-arounds */
  3178. if (before(tp->frto_highmark, tp->snd_una))
  3179. tp->frto_highmark = 0;
  3180. if (tcp_ack_is_dubious(sk, flag)) {
  3181. /* Advance CWND, if state allows this. */
  3182. if ((flag & FLAG_DATA_ACKED) && !frto_cwnd &&
  3183. tcp_may_raise_cwnd(sk, flag))
  3184. tcp_cong_avoid(sk, ack, prior_in_flight);
  3185. is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
  3186. tcp_fastretrans_alert(sk, pkts_acked, newly_acked_sacked,
  3187. is_dupack, flag);
  3188. } else {
  3189. if ((flag & FLAG_DATA_ACKED) && !frto_cwnd)
  3190. tcp_cong_avoid(sk, ack, prior_in_flight);
  3191. }
  3192. if ((flag & FLAG_FORWARD_PROGRESS) || !(flag & FLAG_NOT_DUP)) {
  3193. struct dst_entry *dst = __sk_dst_get(sk);
  3194. if (dst)
  3195. dst_confirm(dst);
  3196. }
  3197. return 1;
  3198. no_queue:
  3199. /* If data was DSACKed, see if we can undo a cwnd reduction. */
  3200. if (flag & FLAG_DSACKING_ACK)
  3201. tcp_fastretrans_alert(sk, pkts_acked, newly_acked_sacked,
  3202. is_dupack, flag);
  3203. /* If this ack opens up a zero window, clear backoff. It was
  3204. * being used to time the probes, and is probably far higher than
  3205. * it needs to be for normal retransmission.
  3206. */
  3207. if (tcp_send_head(sk))
  3208. tcp_ack_probe(sk);
  3209. return 1;
  3210. invalid_ack:
  3211. SOCK_DEBUG(sk, "Ack %u after %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
  3212. return -1;
  3213. old_ack:
  3214. /* If data was SACKed, tag it and see if we should send more data.
  3215. * If data was DSACKed, see if we can undo a cwnd reduction.
  3216. */
  3217. if (TCP_SKB_CB(skb)->sacked) {
  3218. flag |= tcp_sacktag_write_queue(sk, skb, prior_snd_una);
  3219. newly_acked_sacked = tp->sacked_out - prior_sacked;
  3220. tcp_fastretrans_alert(sk, pkts_acked, newly_acked_sacked,
  3221. is_dupack, flag);
  3222. }
  3223. SOCK_DEBUG(sk, "Ack %u before %u:%u\n", ack, tp->snd_una, tp->snd_nxt);
  3224. return 0;
  3225. }
  3226. /* Look for tcp options. Normally only called on SYN and SYNACK packets.
  3227. * But, this can also be called on packets in the established flow when
  3228. * the fast version below fails.
  3229. */
  3230. void tcp_parse_options(const struct sk_buff *skb, struct tcp_options_received *opt_rx,
  3231. const u8 **hvpp, int estab,
  3232. struct tcp_fastopen_cookie *foc)
  3233. {
  3234. const unsigned char *ptr;
  3235. const struct tcphdr *th = tcp_hdr(skb);
  3236. int length = (th->doff * 4) - sizeof(struct tcphdr);
  3237. ptr = (const unsigned char *)(th + 1);
  3238. opt_rx->saw_tstamp = 0;
  3239. while (length > 0) {
  3240. int opcode = *ptr++;
  3241. int opsize;
  3242. switch (opcode) {
  3243. case TCPOPT_EOL:
  3244. return;
  3245. case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */
  3246. length--;
  3247. continue;
  3248. default:
  3249. opsize = *ptr++;
  3250. if (opsize < 2) /* "silly options" */
  3251. return;
  3252. if (opsize > length)
  3253. return; /* don't parse partial options */
  3254. switch (opcode) {
  3255. case TCPOPT_MSS:
  3256. if (opsize == TCPOLEN_MSS && th->syn && !estab) {
  3257. u16 in_mss = get_unaligned_be16(ptr);
  3258. if (in_mss) {
  3259. if (opt_rx->user_mss &&
  3260. opt_rx->user_mss < in_mss)
  3261. in_mss = opt_rx->user_mss;
  3262. opt_rx->mss_clamp = in_mss;
  3263. }
  3264. }
  3265. break;
  3266. case TCPOPT_WINDOW:
  3267. if (opsize == TCPOLEN_WINDOW && th->syn &&
  3268. !estab && sysctl_tcp_window_scaling) {
  3269. __u8 snd_wscale = *(__u8 *)ptr;
  3270. opt_rx->wscale_ok = 1;
  3271. if (snd_wscale > 14) {
  3272. net_info_ratelimited("%s: Illegal window scaling value %d >14 received\n",
  3273. __func__,
  3274. snd_wscale);
  3275. snd_wscale = 14;
  3276. }
  3277. opt_rx->snd_wscale = snd_wscale;
  3278. }
  3279. break;
  3280. case TCPOPT_TIMESTAMP:
  3281. if ((opsize == TCPOLEN_TIMESTAMP) &&
  3282. ((estab && opt_rx->tstamp_ok) ||
  3283. (!estab && sysctl_tcp_timestamps))) {
  3284. opt_rx->saw_tstamp = 1;
  3285. opt_rx->rcv_tsval = get_unaligned_be32(ptr);
  3286. opt_rx->rcv_tsecr = get_unaligned_be32(ptr + 4);
  3287. }
  3288. break;
  3289. case TCPOPT_SACK_PERM:
  3290. if (opsize == TCPOLEN_SACK_PERM && th->syn &&
  3291. !estab && sysctl_tcp_sack) {
  3292. opt_rx->sack_ok = TCP_SACK_SEEN;
  3293. tcp_sack_reset(opt_rx);
  3294. }
  3295. break;
  3296. case TCPOPT_SACK:
  3297. if ((opsize >= (TCPOLEN_SACK_BASE + TCPOLEN_SACK_PERBLOCK)) &&
  3298. !((opsize - TCPOLEN_SACK_BASE) % TCPOLEN_SACK_PERBLOCK) &&
  3299. opt_rx->sack_ok) {
  3300. TCP_SKB_CB(skb)->sacked = (ptr - 2) - (unsigned char *)th;
  3301. }
  3302. break;
  3303. #ifdef CONFIG_TCP_MD5SIG
  3304. case TCPOPT_MD5SIG:
  3305. /*
  3306. * The MD5 Hash has already been
  3307. * checked (see tcp_v{4,6}_do_rcv()).
  3308. */
  3309. break;
  3310. #endif
  3311. case TCPOPT_COOKIE:
  3312. /* This option is variable length.
  3313. */
  3314. switch (opsize) {
  3315. case TCPOLEN_COOKIE_BASE:
  3316. /* not yet implemented */
  3317. break;
  3318. case TCPOLEN_COOKIE_PAIR:
  3319. /* not yet implemented */
  3320. break;
  3321. case TCPOLEN_COOKIE_MIN+0:
  3322. case TCPOLEN_COOKIE_MIN+2:
  3323. case TCPOLEN_COOKIE_MIN+4:
  3324. case TCPOLEN_COOKIE_MIN+6:
  3325. case TCPOLEN_COOKIE_MAX:
  3326. /* 16-bit multiple */
  3327. opt_rx->cookie_plus = opsize;
  3328. *hvpp = ptr;
  3329. break;
  3330. default:
  3331. /* ignore option */
  3332. break;
  3333. }
  3334. break;
  3335. case TCPOPT_EXP:
  3336. /* Fast Open option shares code 254 using a
  3337. * 16 bits magic number. It's valid only in
  3338. * SYN or SYN-ACK with an even size.
  3339. */
  3340. if (opsize < TCPOLEN_EXP_FASTOPEN_BASE ||
  3341. get_unaligned_be16(ptr) != TCPOPT_FASTOPEN_MAGIC ||
  3342. foc == NULL || !th->syn || (opsize & 1))
  3343. break;
  3344. foc->len = opsize - TCPOLEN_EXP_FASTOPEN_BASE;
  3345. if (foc->len >= TCP_FASTOPEN_COOKIE_MIN &&
  3346. foc->len <= TCP_FASTOPEN_COOKIE_MAX)
  3347. memcpy(foc->val, ptr + 2, foc->len);
  3348. else if (foc->len != 0)
  3349. foc->len = -1;
  3350. break;
  3351. }
  3352. ptr += opsize-2;
  3353. length -= opsize;
  3354. }
  3355. }
  3356. }
  3357. EXPORT_SYMBOL(tcp_parse_options);
  3358. static bool tcp_parse_aligned_timestamp(struct tcp_sock *tp, const struct tcphdr *th)
  3359. {
  3360. const __be32 *ptr = (const __be32 *)(th + 1);
  3361. if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
  3362. | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
  3363. tp->rx_opt.saw_tstamp = 1;
  3364. ++ptr;
  3365. tp->rx_opt.rcv_tsval = ntohl(*ptr);
  3366. ++ptr;
  3367. tp->rx_opt.rcv_tsecr = ntohl(*ptr);
  3368. return true;
  3369. }
  3370. return false;
  3371. }
  3372. /* Fast parse options. This hopes to only see timestamps.
  3373. * If it is wrong it falls back on tcp_parse_options().
  3374. */
  3375. static bool tcp_fast_parse_options(const struct sk_buff *skb,
  3376. const struct tcphdr *th,
  3377. struct tcp_sock *tp, const u8 **hvpp)
  3378. {
  3379. /* In the spirit of fast parsing, compare doff directly to constant
  3380. * values. Because equality is used, short doff can be ignored here.
  3381. */
  3382. if (th->doff == (sizeof(*th) / 4)) {
  3383. tp->rx_opt.saw_tstamp = 0;
  3384. return false;
  3385. } else if (tp->rx_opt.tstamp_ok &&
  3386. th->doff == ((sizeof(*th) + TCPOLEN_TSTAMP_ALIGNED) / 4)) {
  3387. if (tcp_parse_aligned_timestamp(tp, th))
  3388. return true;
  3389. }
  3390. tcp_parse_options(skb, &tp->rx_opt, hvpp, 1, NULL);
  3391. return true;
  3392. }
  3393. #ifdef CONFIG_TCP_MD5SIG
  3394. /*
  3395. * Parse MD5 Signature option
  3396. */
  3397. const u8 *tcp_parse_md5sig_option(const struct tcphdr *th)
  3398. {
  3399. int length = (th->doff << 2) - sizeof(*th);
  3400. const u8 *ptr = (const u8 *)(th + 1);
  3401. /* If the TCP option is too short, we can short cut */
  3402. if (length < TCPOLEN_MD5SIG)
  3403. return NULL;
  3404. while (length > 0) {
  3405. int opcode = *ptr++;
  3406. int opsize;
  3407. switch(opcode) {
  3408. case TCPOPT_EOL:
  3409. return NULL;
  3410. case TCPOPT_NOP:
  3411. length--;
  3412. continue;
  3413. default:
  3414. opsize = *ptr++;
  3415. if (opsize < 2 || opsize > length)
  3416. return NULL;
  3417. if (opcode == TCPOPT_MD5SIG)
  3418. return opsize == TCPOLEN_MD5SIG ? ptr : NULL;
  3419. }
  3420. ptr += opsize - 2;
  3421. length -= opsize;
  3422. }
  3423. return NULL;
  3424. }
  3425. EXPORT_SYMBOL(tcp_parse_md5sig_option);
  3426. #endif
  3427. static inline void tcp_store_ts_recent(struct tcp_sock *tp)
  3428. {
  3429. tp->rx_opt.ts_recent = tp->rx_opt.rcv_tsval;
  3430. tp->rx_opt.ts_recent_stamp = get_seconds();
  3431. }
  3432. static inline void tcp_replace_ts_recent(struct tcp_sock *tp, u32 seq)
  3433. {
  3434. if (tp->rx_opt.saw_tstamp && !after(seq, tp->rcv_wup)) {
  3435. /* PAWS bug workaround wrt. ACK frames, the PAWS discard
  3436. * extra check below makes sure this can only happen
  3437. * for pure ACK frames. -DaveM
  3438. *
  3439. * Not only, also it occurs for expired timestamps.
  3440. */
  3441. if (tcp_paws_check(&tp->rx_opt, 0))
  3442. tcp_store_ts_recent(tp);
  3443. }
  3444. }
  3445. /* Sorry, PAWS as specified is broken wrt. pure-ACKs -DaveM
  3446. *
  3447. * It is not fatal. If this ACK does _not_ change critical state (seqs, window)
  3448. * it can pass through stack. So, the following predicate verifies that
  3449. * this segment is not used for anything but congestion avoidance or
  3450. * fast retransmit. Moreover, we even are able to eliminate most of such
  3451. * second order effects, if we apply some small "replay" window (~RTO)
  3452. * to timestamp space.
  3453. *
  3454. * All these measures still do not guarantee that we reject wrapped ACKs
  3455. * on networks with high bandwidth, when sequence space is recycled fastly,
  3456. * but it guarantees that such events will be very rare and do not affect
  3457. * connection seriously. This doesn't look nice, but alas, PAWS is really
  3458. * buggy extension.
  3459. *
  3460. * [ Later note. Even worse! It is buggy for segments _with_ data. RFC
  3461. * states that events when retransmit arrives after original data are rare.
  3462. * It is a blatant lie. VJ forgot about fast retransmit! 8)8) It is
  3463. * the biggest problem on large power networks even with minor reordering.
  3464. * OK, let's give it small replay window. If peer clock is even 1hz, it is safe
  3465. * up to bandwidth of 18Gigabit/sec. 8) ]
  3466. */
  3467. static int tcp_disordered_ack(const struct sock *sk, const struct sk_buff *skb)
  3468. {
  3469. const struct tcp_sock *tp = tcp_sk(sk);
  3470. const struct tcphdr *th = tcp_hdr(skb);
  3471. u32 seq = TCP_SKB_CB(skb)->seq;
  3472. u32 ack = TCP_SKB_CB(skb)->ack_seq;
  3473. return (/* 1. Pure ACK with correct sequence number. */
  3474. (th->ack && seq == TCP_SKB_CB(skb)->end_seq && seq == tp->rcv_nxt) &&
  3475. /* 2. ... and duplicate ACK. */
  3476. ack == tp->snd_una &&
  3477. /* 3. ... and does not update window. */
  3478. !tcp_may_update_window(tp, ack, seq, ntohs(th->window) << tp->rx_opt.snd_wscale) &&
  3479. /* 4. ... and sits in replay window. */
  3480. (s32)(tp->rx_opt.ts_recent - tp->rx_opt.rcv_tsval) <= (inet_csk(sk)->icsk_rto * 1024) / HZ);
  3481. }
  3482. static inline bool tcp_paws_discard(const struct sock *sk,
  3483. const struct sk_buff *skb)
  3484. {
  3485. const struct tcp_sock *tp = tcp_sk(sk);
  3486. return !tcp_paws_check(&tp->rx_opt, TCP_PAWS_WINDOW) &&
  3487. !tcp_disordered_ack(sk, skb);
  3488. }
  3489. /* Check segment sequence number for validity.
  3490. *
  3491. * Segment controls are considered valid, if the segment
  3492. * fits to the window after truncation to the window. Acceptability
  3493. * of data (and SYN, FIN, of course) is checked separately.
  3494. * See tcp_data_queue(), for example.
  3495. *
  3496. * Also, controls (RST is main one) are accepted using RCV.WUP instead
  3497. * of RCV.NXT. Peer still did not advance his SND.UNA when we
  3498. * delayed ACK, so that hisSND.UNA<=ourRCV.WUP.
  3499. * (borrowed from freebsd)
  3500. */
  3501. static inline bool tcp_sequence(const struct tcp_sock *tp, u32 seq, u32 end_seq)
  3502. {
  3503. return !before(end_seq, tp->rcv_wup) &&
  3504. !after(seq, tp->rcv_nxt + tcp_receive_window(tp));
  3505. }
  3506. /* When we get a reset we do this. */
  3507. static void tcp_reset(struct sock *sk)
  3508. {
  3509. /* We want the right error as BSD sees it (and indeed as we do). */
  3510. switch (sk->sk_state) {
  3511. case TCP_SYN_SENT:
  3512. sk->sk_err = ECONNREFUSED;
  3513. break;
  3514. case TCP_CLOSE_WAIT:
  3515. sk->sk_err = EPIPE;
  3516. break;
  3517. case TCP_CLOSE:
  3518. return;
  3519. default:
  3520. sk->sk_err = ECONNRESET;
  3521. }
  3522. /* This barrier is coupled with smp_rmb() in tcp_poll() */
  3523. smp_wmb();
  3524. if (!sock_flag(sk, SOCK_DEAD))
  3525. sk->sk_error_report(sk);
  3526. tcp_done(sk);
  3527. }
  3528. /*
  3529. * Process the FIN bit. This now behaves as it is supposed to work
  3530. * and the FIN takes effect when it is validly part of sequence
  3531. * space. Not before when we get holes.
  3532. *
  3533. * If we are ESTABLISHED, a received fin moves us to CLOSE-WAIT
  3534. * (and thence onto LAST-ACK and finally, CLOSE, we never enter
  3535. * TIME-WAIT)
  3536. *
  3537. * If we are in FINWAIT-1, a received FIN indicates simultaneous
  3538. * close and we go into CLOSING (and later onto TIME-WAIT)
  3539. *
  3540. * If we are in FINWAIT-2, a received FIN moves us to TIME-WAIT.
  3541. */
  3542. static void tcp_fin(struct sock *sk)
  3543. {
  3544. struct tcp_sock *tp = tcp_sk(sk);
  3545. inet_csk_schedule_ack(sk);
  3546. sk->sk_shutdown |= RCV_SHUTDOWN;
  3547. sock_set_flag(sk, SOCK_DONE);
  3548. switch (sk->sk_state) {
  3549. case TCP_SYN_RECV:
  3550. case TCP_ESTABLISHED:
  3551. /* Move to CLOSE_WAIT */
  3552. tcp_set_state(sk, TCP_CLOSE_WAIT);
  3553. inet_csk(sk)->icsk_ack.pingpong = 1;
  3554. break;
  3555. case TCP_CLOSE_WAIT:
  3556. case TCP_CLOSING:
  3557. /* Received a retransmission of the FIN, do
  3558. * nothing.
  3559. */
  3560. break;
  3561. case TCP_LAST_ACK:
  3562. /* RFC793: Remain in the LAST-ACK state. */
  3563. break;
  3564. case TCP_FIN_WAIT1:
  3565. /* This case occurs when a simultaneous close
  3566. * happens, we must ack the received FIN and
  3567. * enter the CLOSING state.
  3568. */
  3569. tcp_send_ack(sk);
  3570. tcp_set_state(sk, TCP_CLOSING);
  3571. break;
  3572. case TCP_FIN_WAIT2:
  3573. /* Received a FIN -- send ACK and enter TIME_WAIT. */
  3574. tcp_send_ack(sk);
  3575. tcp_time_wait(sk, TCP_TIME_WAIT, 0);
  3576. break;
  3577. default:
  3578. /* Only TCP_LISTEN and TCP_CLOSE are left, in these
  3579. * cases we should never reach this piece of code.
  3580. */
  3581. pr_err("%s: Impossible, sk->sk_state=%d\n",
  3582. __func__, sk->sk_state);
  3583. break;
  3584. }
  3585. /* It _is_ possible, that we have something out-of-order _after_ FIN.
  3586. * Probably, we should reset in this case. For now drop them.
  3587. */
  3588. __skb_queue_purge(&tp->out_of_order_queue);
  3589. if (tcp_is_sack(tp))
  3590. tcp_sack_reset(&tp->rx_opt);
  3591. sk_mem_reclaim(sk);
  3592. if (!sock_flag(sk, SOCK_DEAD)) {
  3593. sk->sk_state_change(sk);
  3594. /* Do not send POLL_HUP for half duplex close. */
  3595. if (sk->sk_shutdown == SHUTDOWN_MASK ||
  3596. sk->sk_state == TCP_CLOSE)
  3597. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_HUP);
  3598. else
  3599. sk_wake_async(sk, SOCK_WAKE_WAITD, POLL_IN);
  3600. }
  3601. }
  3602. static inline bool tcp_sack_extend(struct tcp_sack_block *sp, u32 seq,
  3603. u32 end_seq)
  3604. {
  3605. if (!after(seq, sp->end_seq) && !after(sp->start_seq, end_seq)) {
  3606. if (before(seq, sp->start_seq))
  3607. sp->start_seq = seq;
  3608. if (after(end_seq, sp->end_seq))
  3609. sp->end_seq = end_seq;
  3610. return true;
  3611. }
  3612. return false;
  3613. }
  3614. static void tcp_dsack_set(struct sock *sk, u32 seq, u32 end_seq)
  3615. {
  3616. struct tcp_sock *tp = tcp_sk(sk);
  3617. if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
  3618. int mib_idx;
  3619. if (before(seq, tp->rcv_nxt))
  3620. mib_idx = LINUX_MIB_TCPDSACKOLDSENT;
  3621. else
  3622. mib_idx = LINUX_MIB_TCPDSACKOFOSENT;
  3623. NET_INC_STATS_BH(sock_net(sk), mib_idx);
  3624. tp->rx_opt.dsack = 1;
  3625. tp->duplicate_sack[0].start_seq = seq;
  3626. tp->duplicate_sack[0].end_seq = end_seq;
  3627. }
  3628. }
  3629. static void tcp_dsack_extend(struct sock *sk, u32 seq, u32 end_seq)
  3630. {
  3631. struct tcp_sock *tp = tcp_sk(sk);
  3632. if (!tp->rx_opt.dsack)
  3633. tcp_dsack_set(sk, seq, end_seq);
  3634. else
  3635. tcp_sack_extend(tp->duplicate_sack, seq, end_seq);
  3636. }
  3637. static void tcp_send_dupack(struct sock *sk, const struct sk_buff *skb)
  3638. {
  3639. struct tcp_sock *tp = tcp_sk(sk);
  3640. if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
  3641. before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
  3642. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
  3643. tcp_enter_quickack_mode(sk);
  3644. if (tcp_is_sack(tp) && sysctl_tcp_dsack) {
  3645. u32 end_seq = TCP_SKB_CB(skb)->end_seq;
  3646. if (after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt))
  3647. end_seq = tp->rcv_nxt;
  3648. tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, end_seq);
  3649. }
  3650. }
  3651. tcp_send_ack(sk);
  3652. }
  3653. /* These routines update the SACK block as out-of-order packets arrive or
  3654. * in-order packets close up the sequence space.
  3655. */
  3656. static void tcp_sack_maybe_coalesce(struct tcp_sock *tp)
  3657. {
  3658. int this_sack;
  3659. struct tcp_sack_block *sp = &tp->selective_acks[0];
  3660. struct tcp_sack_block *swalk = sp + 1;
  3661. /* See if the recent change to the first SACK eats into
  3662. * or hits the sequence space of other SACK blocks, if so coalesce.
  3663. */
  3664. for (this_sack = 1; this_sack < tp->rx_opt.num_sacks;) {
  3665. if (tcp_sack_extend(sp, swalk->start_seq, swalk->end_seq)) {
  3666. int i;
  3667. /* Zap SWALK, by moving every further SACK up by one slot.
  3668. * Decrease num_sacks.
  3669. */
  3670. tp->rx_opt.num_sacks--;
  3671. for (i = this_sack; i < tp->rx_opt.num_sacks; i++)
  3672. sp[i] = sp[i + 1];
  3673. continue;
  3674. }
  3675. this_sack++, swalk++;
  3676. }
  3677. }
  3678. static void tcp_sack_new_ofo_skb(struct sock *sk, u32 seq, u32 end_seq)
  3679. {
  3680. struct tcp_sock *tp = tcp_sk(sk);
  3681. struct tcp_sack_block *sp = &tp->selective_acks[0];
  3682. int cur_sacks = tp->rx_opt.num_sacks;
  3683. int this_sack;
  3684. if (!cur_sacks)
  3685. goto new_sack;
  3686. for (this_sack = 0; this_sack < cur_sacks; this_sack++, sp++) {
  3687. if (tcp_sack_extend(sp, seq, end_seq)) {
  3688. /* Rotate this_sack to the first one. */
  3689. for (; this_sack > 0; this_sack--, sp--)
  3690. swap(*sp, *(sp - 1));
  3691. if (cur_sacks > 1)
  3692. tcp_sack_maybe_coalesce(tp);
  3693. return;
  3694. }
  3695. }
  3696. /* Could not find an adjacent existing SACK, build a new one,
  3697. * put it at the front, and shift everyone else down. We
  3698. * always know there is at least one SACK present already here.
  3699. *
  3700. * If the sack array is full, forget about the last one.
  3701. */
  3702. if (this_sack >= TCP_NUM_SACKS) {
  3703. this_sack--;
  3704. tp->rx_opt.num_sacks--;
  3705. sp--;
  3706. }
  3707. for (; this_sack > 0; this_sack--, sp--)
  3708. *sp = *(sp - 1);
  3709. new_sack:
  3710. /* Build the new head SACK, and we're done. */
  3711. sp->start_seq = seq;
  3712. sp->end_seq = end_seq;
  3713. tp->rx_opt.num_sacks++;
  3714. }
  3715. /* RCV.NXT advances, some SACKs should be eaten. */
  3716. static void tcp_sack_remove(struct tcp_sock *tp)
  3717. {
  3718. struct tcp_sack_block *sp = &tp->selective_acks[0];
  3719. int num_sacks = tp->rx_opt.num_sacks;
  3720. int this_sack;
  3721. /* Empty ofo queue, hence, all the SACKs are eaten. Clear. */
  3722. if (skb_queue_empty(&tp->out_of_order_queue)) {
  3723. tp->rx_opt.num_sacks = 0;
  3724. return;
  3725. }
  3726. for (this_sack = 0; this_sack < num_sacks;) {
  3727. /* Check if the start of the sack is covered by RCV.NXT. */
  3728. if (!before(tp->rcv_nxt, sp->start_seq)) {
  3729. int i;
  3730. /* RCV.NXT must cover all the block! */
  3731. WARN_ON(before(tp->rcv_nxt, sp->end_seq));
  3732. /* Zap this SACK, by moving forward any other SACKS. */
  3733. for (i=this_sack+1; i < num_sacks; i++)
  3734. tp->selective_acks[i-1] = tp->selective_acks[i];
  3735. num_sacks--;
  3736. continue;
  3737. }
  3738. this_sack++;
  3739. sp++;
  3740. }
  3741. tp->rx_opt.num_sacks = num_sacks;
  3742. }
  3743. /* This one checks to see if we can put data from the
  3744. * out_of_order queue into the receive_queue.
  3745. */
  3746. static void tcp_ofo_queue(struct sock *sk)
  3747. {
  3748. struct tcp_sock *tp = tcp_sk(sk);
  3749. __u32 dsack_high = tp->rcv_nxt;
  3750. struct sk_buff *skb;
  3751. while ((skb = skb_peek(&tp->out_of_order_queue)) != NULL) {
  3752. if (after(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
  3753. break;
  3754. if (before(TCP_SKB_CB(skb)->seq, dsack_high)) {
  3755. __u32 dsack = dsack_high;
  3756. if (before(TCP_SKB_CB(skb)->end_seq, dsack_high))
  3757. dsack_high = TCP_SKB_CB(skb)->end_seq;
  3758. tcp_dsack_extend(sk, TCP_SKB_CB(skb)->seq, dsack);
  3759. }
  3760. if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
  3761. SOCK_DEBUG(sk, "ofo packet was already received\n");
  3762. __skb_unlink(skb, &tp->out_of_order_queue);
  3763. __kfree_skb(skb);
  3764. continue;
  3765. }
  3766. SOCK_DEBUG(sk, "ofo requeuing : rcv_next %X seq %X - %X\n",
  3767. tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
  3768. TCP_SKB_CB(skb)->end_seq);
  3769. __skb_unlink(skb, &tp->out_of_order_queue);
  3770. __skb_queue_tail(&sk->sk_receive_queue, skb);
  3771. tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  3772. if (tcp_hdr(skb)->fin)
  3773. tcp_fin(sk);
  3774. }
  3775. }
  3776. static bool tcp_prune_ofo_queue(struct sock *sk);
  3777. static int tcp_prune_queue(struct sock *sk);
  3778. static int tcp_try_rmem_schedule(struct sock *sk, unsigned int size)
  3779. {
  3780. if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
  3781. !sk_rmem_schedule(sk, size)) {
  3782. if (tcp_prune_queue(sk) < 0)
  3783. return -1;
  3784. if (!sk_rmem_schedule(sk, size)) {
  3785. if (!tcp_prune_ofo_queue(sk))
  3786. return -1;
  3787. if (!sk_rmem_schedule(sk, size))
  3788. return -1;
  3789. }
  3790. }
  3791. return 0;
  3792. }
  3793. /**
  3794. * tcp_try_coalesce - try to merge skb to prior one
  3795. * @sk: socket
  3796. * @to: prior buffer
  3797. * @from: buffer to add in queue
  3798. * @fragstolen: pointer to boolean
  3799. *
  3800. * Before queueing skb @from after @to, try to merge them
  3801. * to reduce overall memory use and queue lengths, if cost is small.
  3802. * Packets in ofo or receive queues can stay a long time.
  3803. * Better try to coalesce them right now to avoid future collapses.
  3804. * Returns true if caller should free @from instead of queueing it
  3805. */
  3806. static bool tcp_try_coalesce(struct sock *sk,
  3807. struct sk_buff *to,
  3808. struct sk_buff *from,
  3809. bool *fragstolen)
  3810. {
  3811. int delta;
  3812. *fragstolen = false;
  3813. if (tcp_hdr(from)->fin)
  3814. return false;
  3815. /* Its possible this segment overlaps with prior segment in queue */
  3816. if (TCP_SKB_CB(from)->seq != TCP_SKB_CB(to)->end_seq)
  3817. return false;
  3818. if (!skb_try_coalesce(to, from, fragstolen, &delta))
  3819. return false;
  3820. atomic_add(delta, &sk->sk_rmem_alloc);
  3821. sk_mem_charge(sk, delta);
  3822. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOALESCE);
  3823. TCP_SKB_CB(to)->end_seq = TCP_SKB_CB(from)->end_seq;
  3824. TCP_SKB_CB(to)->ack_seq = TCP_SKB_CB(from)->ack_seq;
  3825. return true;
  3826. }
  3827. static void tcp_data_queue_ofo(struct sock *sk, struct sk_buff *skb)
  3828. {
  3829. struct tcp_sock *tp = tcp_sk(sk);
  3830. struct sk_buff *skb1;
  3831. u32 seq, end_seq;
  3832. TCP_ECN_check_ce(tp, skb);
  3833. if (unlikely(tcp_try_rmem_schedule(sk, skb->truesize))) {
  3834. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFODROP);
  3835. __kfree_skb(skb);
  3836. return;
  3837. }
  3838. /* Disable header prediction. */
  3839. tp->pred_flags = 0;
  3840. inet_csk_schedule_ack(sk);
  3841. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOQUEUE);
  3842. SOCK_DEBUG(sk, "out of order segment: rcv_next %X seq %X - %X\n",
  3843. tp->rcv_nxt, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
  3844. skb1 = skb_peek_tail(&tp->out_of_order_queue);
  3845. if (!skb1) {
  3846. /* Initial out of order segment, build 1 SACK. */
  3847. if (tcp_is_sack(tp)) {
  3848. tp->rx_opt.num_sacks = 1;
  3849. tp->selective_acks[0].start_seq = TCP_SKB_CB(skb)->seq;
  3850. tp->selective_acks[0].end_seq =
  3851. TCP_SKB_CB(skb)->end_seq;
  3852. }
  3853. __skb_queue_head(&tp->out_of_order_queue, skb);
  3854. goto end;
  3855. }
  3856. seq = TCP_SKB_CB(skb)->seq;
  3857. end_seq = TCP_SKB_CB(skb)->end_seq;
  3858. if (seq == TCP_SKB_CB(skb1)->end_seq) {
  3859. bool fragstolen;
  3860. if (!tcp_try_coalesce(sk, skb1, skb, &fragstolen)) {
  3861. __skb_queue_after(&tp->out_of_order_queue, skb1, skb);
  3862. } else {
  3863. kfree_skb_partial(skb, fragstolen);
  3864. skb = NULL;
  3865. }
  3866. if (!tp->rx_opt.num_sacks ||
  3867. tp->selective_acks[0].end_seq != seq)
  3868. goto add_sack;
  3869. /* Common case: data arrive in order after hole. */
  3870. tp->selective_acks[0].end_seq = end_seq;
  3871. goto end;
  3872. }
  3873. /* Find place to insert this segment. */
  3874. while (1) {
  3875. if (!after(TCP_SKB_CB(skb1)->seq, seq))
  3876. break;
  3877. if (skb_queue_is_first(&tp->out_of_order_queue, skb1)) {
  3878. skb1 = NULL;
  3879. break;
  3880. }
  3881. skb1 = skb_queue_prev(&tp->out_of_order_queue, skb1);
  3882. }
  3883. /* Do skb overlap to previous one? */
  3884. if (skb1 && before(seq, TCP_SKB_CB(skb1)->end_seq)) {
  3885. if (!after(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
  3886. /* All the bits are present. Drop. */
  3887. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
  3888. __kfree_skb(skb);
  3889. skb = NULL;
  3890. tcp_dsack_set(sk, seq, end_seq);
  3891. goto add_sack;
  3892. }
  3893. if (after(seq, TCP_SKB_CB(skb1)->seq)) {
  3894. /* Partial overlap. */
  3895. tcp_dsack_set(sk, seq,
  3896. TCP_SKB_CB(skb1)->end_seq);
  3897. } else {
  3898. if (skb_queue_is_first(&tp->out_of_order_queue,
  3899. skb1))
  3900. skb1 = NULL;
  3901. else
  3902. skb1 = skb_queue_prev(
  3903. &tp->out_of_order_queue,
  3904. skb1);
  3905. }
  3906. }
  3907. if (!skb1)
  3908. __skb_queue_head(&tp->out_of_order_queue, skb);
  3909. else
  3910. __skb_queue_after(&tp->out_of_order_queue, skb1, skb);
  3911. /* And clean segments covered by new one as whole. */
  3912. while (!skb_queue_is_last(&tp->out_of_order_queue, skb)) {
  3913. skb1 = skb_queue_next(&tp->out_of_order_queue, skb);
  3914. if (!after(end_seq, TCP_SKB_CB(skb1)->seq))
  3915. break;
  3916. if (before(end_seq, TCP_SKB_CB(skb1)->end_seq)) {
  3917. tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
  3918. end_seq);
  3919. break;
  3920. }
  3921. __skb_unlink(skb1, &tp->out_of_order_queue);
  3922. tcp_dsack_extend(sk, TCP_SKB_CB(skb1)->seq,
  3923. TCP_SKB_CB(skb1)->end_seq);
  3924. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPOFOMERGE);
  3925. __kfree_skb(skb1);
  3926. }
  3927. add_sack:
  3928. if (tcp_is_sack(tp))
  3929. tcp_sack_new_ofo_skb(sk, seq, end_seq);
  3930. end:
  3931. if (skb)
  3932. skb_set_owner_r(skb, sk);
  3933. }
  3934. static int __must_check tcp_queue_rcv(struct sock *sk, struct sk_buff *skb, int hdrlen,
  3935. bool *fragstolen)
  3936. {
  3937. int eaten;
  3938. struct sk_buff *tail = skb_peek_tail(&sk->sk_receive_queue);
  3939. __skb_pull(skb, hdrlen);
  3940. eaten = (tail &&
  3941. tcp_try_coalesce(sk, tail, skb, fragstolen)) ? 1 : 0;
  3942. tcp_sk(sk)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  3943. if (!eaten) {
  3944. __skb_queue_tail(&sk->sk_receive_queue, skb);
  3945. skb_set_owner_r(skb, sk);
  3946. }
  3947. return eaten;
  3948. }
  3949. int tcp_send_rcvq(struct sock *sk, struct msghdr *msg, size_t size)
  3950. {
  3951. struct sk_buff *skb;
  3952. struct tcphdr *th;
  3953. bool fragstolen;
  3954. if (tcp_try_rmem_schedule(sk, size + sizeof(*th)))
  3955. goto err;
  3956. skb = alloc_skb(size + sizeof(*th), sk->sk_allocation);
  3957. if (!skb)
  3958. goto err;
  3959. th = (struct tcphdr *)skb_put(skb, sizeof(*th));
  3960. skb_reset_transport_header(skb);
  3961. memset(th, 0, sizeof(*th));
  3962. if (memcpy_fromiovec(skb_put(skb, size), msg->msg_iov, size))
  3963. goto err_free;
  3964. TCP_SKB_CB(skb)->seq = tcp_sk(sk)->rcv_nxt;
  3965. TCP_SKB_CB(skb)->end_seq = TCP_SKB_CB(skb)->seq + size;
  3966. TCP_SKB_CB(skb)->ack_seq = tcp_sk(sk)->snd_una - 1;
  3967. if (tcp_queue_rcv(sk, skb, sizeof(*th), &fragstolen)) {
  3968. WARN_ON_ONCE(fragstolen); /* should not happen */
  3969. __kfree_skb(skb);
  3970. }
  3971. return size;
  3972. err_free:
  3973. kfree_skb(skb);
  3974. err:
  3975. return -ENOMEM;
  3976. }
  3977. static void tcp_data_queue(struct sock *sk, struct sk_buff *skb)
  3978. {
  3979. const struct tcphdr *th = tcp_hdr(skb);
  3980. struct tcp_sock *tp = tcp_sk(sk);
  3981. int eaten = -1;
  3982. bool fragstolen = false;
  3983. if (TCP_SKB_CB(skb)->seq == TCP_SKB_CB(skb)->end_seq)
  3984. goto drop;
  3985. skb_dst_drop(skb);
  3986. __skb_pull(skb, th->doff * 4);
  3987. TCP_ECN_accept_cwr(tp, skb);
  3988. tp->rx_opt.dsack = 0;
  3989. /* Queue data for delivery to the user.
  3990. * Packets in sequence go to the receive queue.
  3991. * Out of sequence packets to the out_of_order_queue.
  3992. */
  3993. if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt) {
  3994. if (tcp_receive_window(tp) == 0)
  3995. goto out_of_window;
  3996. /* Ok. In sequence. In window. */
  3997. if (tp->ucopy.task == current &&
  3998. tp->copied_seq == tp->rcv_nxt && tp->ucopy.len &&
  3999. sock_owned_by_user(sk) && !tp->urg_data) {
  4000. int chunk = min_t(unsigned int, skb->len,
  4001. tp->ucopy.len);
  4002. __set_current_state(TASK_RUNNING);
  4003. local_bh_enable();
  4004. if (!skb_copy_datagram_iovec(skb, 0, tp->ucopy.iov, chunk)) {
  4005. tp->ucopy.len -= chunk;
  4006. tp->copied_seq += chunk;
  4007. eaten = (chunk == skb->len);
  4008. tcp_rcv_space_adjust(sk);
  4009. }
  4010. local_bh_disable();
  4011. }
  4012. if (eaten <= 0) {
  4013. queue_and_out:
  4014. if (eaten < 0 &&
  4015. tcp_try_rmem_schedule(sk, skb->truesize))
  4016. goto drop;
  4017. eaten = tcp_queue_rcv(sk, skb, 0, &fragstolen);
  4018. }
  4019. tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  4020. if (skb->len)
  4021. tcp_event_data_recv(sk, skb);
  4022. if (th->fin)
  4023. tcp_fin(sk);
  4024. if (!skb_queue_empty(&tp->out_of_order_queue)) {
  4025. tcp_ofo_queue(sk);
  4026. /* RFC2581. 4.2. SHOULD send immediate ACK, when
  4027. * gap in queue is filled.
  4028. */
  4029. if (skb_queue_empty(&tp->out_of_order_queue))
  4030. inet_csk(sk)->icsk_ack.pingpong = 0;
  4031. }
  4032. if (tp->rx_opt.num_sacks)
  4033. tcp_sack_remove(tp);
  4034. tcp_fast_path_check(sk);
  4035. if (eaten > 0)
  4036. kfree_skb_partial(skb, fragstolen);
  4037. else if (!sock_flag(sk, SOCK_DEAD))
  4038. sk->sk_data_ready(sk, 0);
  4039. return;
  4040. }
  4041. if (!after(TCP_SKB_CB(skb)->end_seq, tp->rcv_nxt)) {
  4042. /* A retransmit, 2nd most common case. Force an immediate ack. */
  4043. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_DELAYEDACKLOST);
  4044. tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq);
  4045. out_of_window:
  4046. tcp_enter_quickack_mode(sk);
  4047. inet_csk_schedule_ack(sk);
  4048. drop:
  4049. __kfree_skb(skb);
  4050. return;
  4051. }
  4052. /* Out of window. F.e. zero window probe. */
  4053. if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt + tcp_receive_window(tp)))
  4054. goto out_of_window;
  4055. tcp_enter_quickack_mode(sk);
  4056. if (before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt)) {
  4057. /* Partial packet, seq < rcv_next < end_seq */
  4058. SOCK_DEBUG(sk, "partial packet: rcv_next %X seq %X - %X\n",
  4059. tp->rcv_nxt, TCP_SKB_CB(skb)->seq,
  4060. TCP_SKB_CB(skb)->end_seq);
  4061. tcp_dsack_set(sk, TCP_SKB_CB(skb)->seq, tp->rcv_nxt);
  4062. /* If window is closed, drop tail of packet. But after
  4063. * remembering D-SACK for its head made in previous line.
  4064. */
  4065. if (!tcp_receive_window(tp))
  4066. goto out_of_window;
  4067. goto queue_and_out;
  4068. }
  4069. tcp_data_queue_ofo(sk, skb);
  4070. }
  4071. static struct sk_buff *tcp_collapse_one(struct sock *sk, struct sk_buff *skb,
  4072. struct sk_buff_head *list)
  4073. {
  4074. struct sk_buff *next = NULL;
  4075. if (!skb_queue_is_last(list, skb))
  4076. next = skb_queue_next(list, skb);
  4077. __skb_unlink(skb, list);
  4078. __kfree_skb(skb);
  4079. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPRCVCOLLAPSED);
  4080. return next;
  4081. }
  4082. /* Collapse contiguous sequence of skbs head..tail with
  4083. * sequence numbers start..end.
  4084. *
  4085. * If tail is NULL, this means until the end of the list.
  4086. *
  4087. * Segments with FIN/SYN are not collapsed (only because this
  4088. * simplifies code)
  4089. */
  4090. static void
  4091. tcp_collapse(struct sock *sk, struct sk_buff_head *list,
  4092. struct sk_buff *head, struct sk_buff *tail,
  4093. u32 start, u32 end)
  4094. {
  4095. struct sk_buff *skb, *n;
  4096. bool end_of_skbs;
  4097. /* First, check that queue is collapsible and find
  4098. * the point where collapsing can be useful. */
  4099. skb = head;
  4100. restart:
  4101. end_of_skbs = true;
  4102. skb_queue_walk_from_safe(list, skb, n) {
  4103. if (skb == tail)
  4104. break;
  4105. /* No new bits? It is possible on ofo queue. */
  4106. if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
  4107. skb = tcp_collapse_one(sk, skb, list);
  4108. if (!skb)
  4109. break;
  4110. goto restart;
  4111. }
  4112. /* The first skb to collapse is:
  4113. * - not SYN/FIN and
  4114. * - bloated or contains data before "start" or
  4115. * overlaps to the next one.
  4116. */
  4117. if (!tcp_hdr(skb)->syn && !tcp_hdr(skb)->fin &&
  4118. (tcp_win_from_space(skb->truesize) > skb->len ||
  4119. before(TCP_SKB_CB(skb)->seq, start))) {
  4120. end_of_skbs = false;
  4121. break;
  4122. }
  4123. if (!skb_queue_is_last(list, skb)) {
  4124. struct sk_buff *next = skb_queue_next(list, skb);
  4125. if (next != tail &&
  4126. TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(next)->seq) {
  4127. end_of_skbs = false;
  4128. break;
  4129. }
  4130. }
  4131. /* Decided to skip this, advance start seq. */
  4132. start = TCP_SKB_CB(skb)->end_seq;
  4133. }
  4134. if (end_of_skbs || tcp_hdr(skb)->syn || tcp_hdr(skb)->fin)
  4135. return;
  4136. while (before(start, end)) {
  4137. struct sk_buff *nskb;
  4138. unsigned int header = skb_headroom(skb);
  4139. int copy = SKB_MAX_ORDER(header, 0);
  4140. /* Too big header? This can happen with IPv6. */
  4141. if (copy < 0)
  4142. return;
  4143. if (end - start < copy)
  4144. copy = end - start;
  4145. nskb = alloc_skb(copy + header, GFP_ATOMIC);
  4146. if (!nskb)
  4147. return;
  4148. skb_set_mac_header(nskb, skb_mac_header(skb) - skb->head);
  4149. skb_set_network_header(nskb, (skb_network_header(skb) -
  4150. skb->head));
  4151. skb_set_transport_header(nskb, (skb_transport_header(skb) -
  4152. skb->head));
  4153. skb_reserve(nskb, header);
  4154. memcpy(nskb->head, skb->head, header);
  4155. memcpy(nskb->cb, skb->cb, sizeof(skb->cb));
  4156. TCP_SKB_CB(nskb)->seq = TCP_SKB_CB(nskb)->end_seq = start;
  4157. __skb_queue_before(list, skb, nskb);
  4158. skb_set_owner_r(nskb, sk);
  4159. /* Copy data, releasing collapsed skbs. */
  4160. while (copy > 0) {
  4161. int offset = start - TCP_SKB_CB(skb)->seq;
  4162. int size = TCP_SKB_CB(skb)->end_seq - start;
  4163. BUG_ON(offset < 0);
  4164. if (size > 0) {
  4165. size = min(copy, size);
  4166. if (skb_copy_bits(skb, offset, skb_put(nskb, size), size))
  4167. BUG();
  4168. TCP_SKB_CB(nskb)->end_seq += size;
  4169. copy -= size;
  4170. start += size;
  4171. }
  4172. if (!before(start, TCP_SKB_CB(skb)->end_seq)) {
  4173. skb = tcp_collapse_one(sk, skb, list);
  4174. if (!skb ||
  4175. skb == tail ||
  4176. tcp_hdr(skb)->syn ||
  4177. tcp_hdr(skb)->fin)
  4178. return;
  4179. }
  4180. }
  4181. }
  4182. }
  4183. /* Collapse ofo queue. Algorithm: select contiguous sequence of skbs
  4184. * and tcp_collapse() them until all the queue is collapsed.
  4185. */
  4186. static void tcp_collapse_ofo_queue(struct sock *sk)
  4187. {
  4188. struct tcp_sock *tp = tcp_sk(sk);
  4189. struct sk_buff *skb = skb_peek(&tp->out_of_order_queue);
  4190. struct sk_buff *head;
  4191. u32 start, end;
  4192. if (skb == NULL)
  4193. return;
  4194. start = TCP_SKB_CB(skb)->seq;
  4195. end = TCP_SKB_CB(skb)->end_seq;
  4196. head = skb;
  4197. for (;;) {
  4198. struct sk_buff *next = NULL;
  4199. if (!skb_queue_is_last(&tp->out_of_order_queue, skb))
  4200. next = skb_queue_next(&tp->out_of_order_queue, skb);
  4201. skb = next;
  4202. /* Segment is terminated when we see gap or when
  4203. * we are at the end of all the queue. */
  4204. if (!skb ||
  4205. after(TCP_SKB_CB(skb)->seq, end) ||
  4206. before(TCP_SKB_CB(skb)->end_seq, start)) {
  4207. tcp_collapse(sk, &tp->out_of_order_queue,
  4208. head, skb, start, end);
  4209. head = skb;
  4210. if (!skb)
  4211. break;
  4212. /* Start new segment */
  4213. start = TCP_SKB_CB(skb)->seq;
  4214. end = TCP_SKB_CB(skb)->end_seq;
  4215. } else {
  4216. if (before(TCP_SKB_CB(skb)->seq, start))
  4217. start = TCP_SKB_CB(skb)->seq;
  4218. if (after(TCP_SKB_CB(skb)->end_seq, end))
  4219. end = TCP_SKB_CB(skb)->end_seq;
  4220. }
  4221. }
  4222. }
  4223. /*
  4224. * Purge the out-of-order queue.
  4225. * Return true if queue was pruned.
  4226. */
  4227. static bool tcp_prune_ofo_queue(struct sock *sk)
  4228. {
  4229. struct tcp_sock *tp = tcp_sk(sk);
  4230. bool res = false;
  4231. if (!skb_queue_empty(&tp->out_of_order_queue)) {
  4232. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_OFOPRUNED);
  4233. __skb_queue_purge(&tp->out_of_order_queue);
  4234. /* Reset SACK state. A conforming SACK implementation will
  4235. * do the same at a timeout based retransmit. When a connection
  4236. * is in a sad state like this, we care only about integrity
  4237. * of the connection not performance.
  4238. */
  4239. if (tp->rx_opt.sack_ok)
  4240. tcp_sack_reset(&tp->rx_opt);
  4241. sk_mem_reclaim(sk);
  4242. res = true;
  4243. }
  4244. return res;
  4245. }
  4246. /* Reduce allocated memory if we can, trying to get
  4247. * the socket within its memory limits again.
  4248. *
  4249. * Return less than zero if we should start dropping frames
  4250. * until the socket owning process reads some of the data
  4251. * to stabilize the situation.
  4252. */
  4253. static int tcp_prune_queue(struct sock *sk)
  4254. {
  4255. struct tcp_sock *tp = tcp_sk(sk);
  4256. SOCK_DEBUG(sk, "prune_queue: c=%x\n", tp->copied_seq);
  4257. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PRUNECALLED);
  4258. if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
  4259. tcp_clamp_window(sk);
  4260. else if (sk_under_memory_pressure(sk))
  4261. tp->rcv_ssthresh = min(tp->rcv_ssthresh, 4U * tp->advmss);
  4262. tcp_collapse_ofo_queue(sk);
  4263. if (!skb_queue_empty(&sk->sk_receive_queue))
  4264. tcp_collapse(sk, &sk->sk_receive_queue,
  4265. skb_peek(&sk->sk_receive_queue),
  4266. NULL,
  4267. tp->copied_seq, tp->rcv_nxt);
  4268. sk_mem_reclaim(sk);
  4269. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
  4270. return 0;
  4271. /* Collapsing did not help, destructive actions follow.
  4272. * This must not ever occur. */
  4273. tcp_prune_ofo_queue(sk);
  4274. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
  4275. return 0;
  4276. /* If we are really being abused, tell the caller to silently
  4277. * drop receive data on the floor. It will get retransmitted
  4278. * and hopefully then we'll have sufficient space.
  4279. */
  4280. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_RCVPRUNED);
  4281. /* Massive buffer overcommit. */
  4282. tp->pred_flags = 0;
  4283. return -1;
  4284. }
  4285. /* RFC2861, slow part. Adjust cwnd, after it was not full during one rto.
  4286. * As additional protections, we do not touch cwnd in retransmission phases,
  4287. * and if application hit its sndbuf limit recently.
  4288. */
  4289. void tcp_cwnd_application_limited(struct sock *sk)
  4290. {
  4291. struct tcp_sock *tp = tcp_sk(sk);
  4292. if (inet_csk(sk)->icsk_ca_state == TCP_CA_Open &&
  4293. sk->sk_socket && !test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
  4294. /* Limited by application or receiver window. */
  4295. u32 init_win = tcp_init_cwnd(tp, __sk_dst_get(sk));
  4296. u32 win_used = max(tp->snd_cwnd_used, init_win);
  4297. if (win_used < tp->snd_cwnd) {
  4298. tp->snd_ssthresh = tcp_current_ssthresh(sk);
  4299. tp->snd_cwnd = (tp->snd_cwnd + win_used) >> 1;
  4300. }
  4301. tp->snd_cwnd_used = 0;
  4302. }
  4303. tp->snd_cwnd_stamp = tcp_time_stamp;
  4304. }
  4305. static bool tcp_should_expand_sndbuf(const struct sock *sk)
  4306. {
  4307. const struct tcp_sock *tp = tcp_sk(sk);
  4308. /* If the user specified a specific send buffer setting, do
  4309. * not modify it.
  4310. */
  4311. if (sk->sk_userlocks & SOCK_SNDBUF_LOCK)
  4312. return false;
  4313. /* If we are under global TCP memory pressure, do not expand. */
  4314. if (sk_under_memory_pressure(sk))
  4315. return false;
  4316. /* If we are under soft global TCP memory pressure, do not expand. */
  4317. if (sk_memory_allocated(sk) >= sk_prot_mem_limits(sk, 0))
  4318. return false;
  4319. /* If we filled the congestion window, do not expand. */
  4320. if (tp->packets_out >= tp->snd_cwnd)
  4321. return false;
  4322. return true;
  4323. }
  4324. /* When incoming ACK allowed to free some skb from write_queue,
  4325. * we remember this event in flag SOCK_QUEUE_SHRUNK and wake up socket
  4326. * on the exit from tcp input handler.
  4327. *
  4328. * PROBLEM: sndbuf expansion does not work well with largesend.
  4329. */
  4330. static void tcp_new_space(struct sock *sk)
  4331. {
  4332. struct tcp_sock *tp = tcp_sk(sk);
  4333. if (tcp_should_expand_sndbuf(sk)) {
  4334. int sndmem = SKB_TRUESIZE(max_t(u32,
  4335. tp->rx_opt.mss_clamp,
  4336. tp->mss_cache) +
  4337. MAX_TCP_HEADER);
  4338. int demanded = max_t(unsigned int, tp->snd_cwnd,
  4339. tp->reordering + 1);
  4340. sndmem *= 2 * demanded;
  4341. if (sndmem > sk->sk_sndbuf)
  4342. sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
  4343. tp->snd_cwnd_stamp = tcp_time_stamp;
  4344. }
  4345. sk->sk_write_space(sk);
  4346. }
  4347. static void tcp_check_space(struct sock *sk)
  4348. {
  4349. if (sock_flag(sk, SOCK_QUEUE_SHRUNK)) {
  4350. sock_reset_flag(sk, SOCK_QUEUE_SHRUNK);
  4351. if (sk->sk_socket &&
  4352. test_bit(SOCK_NOSPACE, &sk->sk_socket->flags))
  4353. tcp_new_space(sk);
  4354. }
  4355. }
  4356. static inline void tcp_data_snd_check(struct sock *sk)
  4357. {
  4358. tcp_push_pending_frames(sk);
  4359. tcp_check_space(sk);
  4360. }
  4361. /*
  4362. * Check if sending an ack is needed.
  4363. */
  4364. static void __tcp_ack_snd_check(struct sock *sk, int ofo_possible)
  4365. {
  4366. struct tcp_sock *tp = tcp_sk(sk);
  4367. /* More than one full frame received... */
  4368. if (((tp->rcv_nxt - tp->rcv_wup) > inet_csk(sk)->icsk_ack.rcv_mss &&
  4369. /* ... and right edge of window advances far enough.
  4370. * (tcp_recvmsg() will send ACK otherwise). Or...
  4371. */
  4372. __tcp_select_window(sk) >= tp->rcv_wnd) ||
  4373. /* We ACK each frame or... */
  4374. tcp_in_quickack_mode(sk) ||
  4375. /* We have out of order data. */
  4376. (ofo_possible && skb_peek(&tp->out_of_order_queue))) {
  4377. /* Then ack it now */
  4378. tcp_send_ack(sk);
  4379. } else {
  4380. /* Else, send delayed ack. */
  4381. tcp_send_delayed_ack(sk);
  4382. }
  4383. }
  4384. static inline void tcp_ack_snd_check(struct sock *sk)
  4385. {
  4386. if (!inet_csk_ack_scheduled(sk)) {
  4387. /* We sent a data segment already. */
  4388. return;
  4389. }
  4390. __tcp_ack_snd_check(sk, 1);
  4391. }
  4392. /*
  4393. * This routine is only called when we have urgent data
  4394. * signaled. Its the 'slow' part of tcp_urg. It could be
  4395. * moved inline now as tcp_urg is only called from one
  4396. * place. We handle URGent data wrong. We have to - as
  4397. * BSD still doesn't use the correction from RFC961.
  4398. * For 1003.1g we should support a new option TCP_STDURG to permit
  4399. * either form (or just set the sysctl tcp_stdurg).
  4400. */
  4401. static void tcp_check_urg(struct sock *sk, const struct tcphdr *th)
  4402. {
  4403. struct tcp_sock *tp = tcp_sk(sk);
  4404. u32 ptr = ntohs(th->urg_ptr);
  4405. if (ptr && !sysctl_tcp_stdurg)
  4406. ptr--;
  4407. ptr += ntohl(th->seq);
  4408. /* Ignore urgent data that we've already seen and read. */
  4409. if (after(tp->copied_seq, ptr))
  4410. return;
  4411. /* Do not replay urg ptr.
  4412. *
  4413. * NOTE: interesting situation not covered by specs.
  4414. * Misbehaving sender may send urg ptr, pointing to segment,
  4415. * which we already have in ofo queue. We are not able to fetch
  4416. * such data and will stay in TCP_URG_NOTYET until will be eaten
  4417. * by recvmsg(). Seems, we are not obliged to handle such wicked
  4418. * situations. But it is worth to think about possibility of some
  4419. * DoSes using some hypothetical application level deadlock.
  4420. */
  4421. if (before(ptr, tp->rcv_nxt))
  4422. return;
  4423. /* Do we already have a newer (or duplicate) urgent pointer? */
  4424. if (tp->urg_data && !after(ptr, tp->urg_seq))
  4425. return;
  4426. /* Tell the world about our new urgent pointer. */
  4427. sk_send_sigurg(sk);
  4428. /* We may be adding urgent data when the last byte read was
  4429. * urgent. To do this requires some care. We cannot just ignore
  4430. * tp->copied_seq since we would read the last urgent byte again
  4431. * as data, nor can we alter copied_seq until this data arrives
  4432. * or we break the semantics of SIOCATMARK (and thus sockatmark())
  4433. *
  4434. * NOTE. Double Dutch. Rendering to plain English: author of comment
  4435. * above did something sort of send("A", MSG_OOB); send("B", MSG_OOB);
  4436. * and expect that both A and B disappear from stream. This is _wrong_.
  4437. * Though this happens in BSD with high probability, this is occasional.
  4438. * Any application relying on this is buggy. Note also, that fix "works"
  4439. * only in this artificial test. Insert some normal data between A and B and we will
  4440. * decline of BSD again. Verdict: it is better to remove to trap
  4441. * buggy users.
  4442. */
  4443. if (tp->urg_seq == tp->copied_seq && tp->urg_data &&
  4444. !sock_flag(sk, SOCK_URGINLINE) && tp->copied_seq != tp->rcv_nxt) {
  4445. struct sk_buff *skb = skb_peek(&sk->sk_receive_queue);
  4446. tp->copied_seq++;
  4447. if (skb && !before(tp->copied_seq, TCP_SKB_CB(skb)->end_seq)) {
  4448. __skb_unlink(skb, &sk->sk_receive_queue);
  4449. __kfree_skb(skb);
  4450. }
  4451. }
  4452. tp->urg_data = TCP_URG_NOTYET;
  4453. tp->urg_seq = ptr;
  4454. /* Disable header prediction. */
  4455. tp->pred_flags = 0;
  4456. }
  4457. /* This is the 'fast' part of urgent handling. */
  4458. static void tcp_urg(struct sock *sk, struct sk_buff *skb, const struct tcphdr *th)
  4459. {
  4460. struct tcp_sock *tp = tcp_sk(sk);
  4461. /* Check if we get a new urgent pointer - normally not. */
  4462. if (th->urg)
  4463. tcp_check_urg(sk, th);
  4464. /* Do we wait for any urgent data? - normally not... */
  4465. if (tp->urg_data == TCP_URG_NOTYET) {
  4466. u32 ptr = tp->urg_seq - ntohl(th->seq) + (th->doff * 4) -
  4467. th->syn;
  4468. /* Is the urgent pointer pointing into this packet? */
  4469. if (ptr < skb->len) {
  4470. u8 tmp;
  4471. if (skb_copy_bits(skb, ptr, &tmp, 1))
  4472. BUG();
  4473. tp->urg_data = TCP_URG_VALID | tmp;
  4474. if (!sock_flag(sk, SOCK_DEAD))
  4475. sk->sk_data_ready(sk, 0);
  4476. }
  4477. }
  4478. }
  4479. static int tcp_copy_to_iovec(struct sock *sk, struct sk_buff *skb, int hlen)
  4480. {
  4481. struct tcp_sock *tp = tcp_sk(sk);
  4482. int chunk = skb->len - hlen;
  4483. int err;
  4484. local_bh_enable();
  4485. if (skb_csum_unnecessary(skb))
  4486. err = skb_copy_datagram_iovec(skb, hlen, tp->ucopy.iov, chunk);
  4487. else
  4488. err = skb_copy_and_csum_datagram_iovec(skb, hlen,
  4489. tp->ucopy.iov);
  4490. if (!err) {
  4491. tp->ucopy.len -= chunk;
  4492. tp->copied_seq += chunk;
  4493. tcp_rcv_space_adjust(sk);
  4494. }
  4495. local_bh_disable();
  4496. return err;
  4497. }
  4498. static __sum16 __tcp_checksum_complete_user(struct sock *sk,
  4499. struct sk_buff *skb)
  4500. {
  4501. __sum16 result;
  4502. if (sock_owned_by_user(sk)) {
  4503. local_bh_enable();
  4504. result = __tcp_checksum_complete(skb);
  4505. local_bh_disable();
  4506. } else {
  4507. result = __tcp_checksum_complete(skb);
  4508. }
  4509. return result;
  4510. }
  4511. static inline bool tcp_checksum_complete_user(struct sock *sk,
  4512. struct sk_buff *skb)
  4513. {
  4514. return !skb_csum_unnecessary(skb) &&
  4515. __tcp_checksum_complete_user(sk, skb);
  4516. }
  4517. #ifdef CONFIG_NET_DMA
  4518. static bool tcp_dma_try_early_copy(struct sock *sk, struct sk_buff *skb,
  4519. int hlen)
  4520. {
  4521. struct tcp_sock *tp = tcp_sk(sk);
  4522. int chunk = skb->len - hlen;
  4523. int dma_cookie;
  4524. bool copied_early = false;
  4525. if (tp->ucopy.wakeup)
  4526. return false;
  4527. if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
  4528. tp->ucopy.dma_chan = net_dma_find_channel();
  4529. if (tp->ucopy.dma_chan && skb_csum_unnecessary(skb)) {
  4530. dma_cookie = dma_skb_copy_datagram_iovec(tp->ucopy.dma_chan,
  4531. skb, hlen,
  4532. tp->ucopy.iov, chunk,
  4533. tp->ucopy.pinned_list);
  4534. if (dma_cookie < 0)
  4535. goto out;
  4536. tp->ucopy.dma_cookie = dma_cookie;
  4537. copied_early = true;
  4538. tp->ucopy.len -= chunk;
  4539. tp->copied_seq += chunk;
  4540. tcp_rcv_space_adjust(sk);
  4541. if ((tp->ucopy.len == 0) ||
  4542. (tcp_flag_word(tcp_hdr(skb)) & TCP_FLAG_PSH) ||
  4543. (atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1))) {
  4544. tp->ucopy.wakeup = 1;
  4545. sk->sk_data_ready(sk, 0);
  4546. }
  4547. } else if (chunk > 0) {
  4548. tp->ucopy.wakeup = 1;
  4549. sk->sk_data_ready(sk, 0);
  4550. }
  4551. out:
  4552. return copied_early;
  4553. }
  4554. #endif /* CONFIG_NET_DMA */
  4555. static void tcp_send_challenge_ack(struct sock *sk)
  4556. {
  4557. /* unprotected vars, we dont care of overwrites */
  4558. static u32 challenge_timestamp;
  4559. static unsigned int challenge_count;
  4560. u32 now = jiffies / HZ;
  4561. if (now != challenge_timestamp) {
  4562. challenge_timestamp = now;
  4563. challenge_count = 0;
  4564. }
  4565. if (++challenge_count <= sysctl_tcp_challenge_ack_limit) {
  4566. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPCHALLENGEACK);
  4567. tcp_send_ack(sk);
  4568. }
  4569. }
  4570. /* Does PAWS and seqno based validation of an incoming segment, flags will
  4571. * play significant role here.
  4572. */
  4573. static bool tcp_validate_incoming(struct sock *sk, struct sk_buff *skb,
  4574. const struct tcphdr *th, int syn_inerr)
  4575. {
  4576. const u8 *hash_location;
  4577. struct tcp_sock *tp = tcp_sk(sk);
  4578. /* RFC1323: H1. Apply PAWS check first. */
  4579. if (tcp_fast_parse_options(skb, th, tp, &hash_location) &&
  4580. tp->rx_opt.saw_tstamp &&
  4581. tcp_paws_discard(sk, skb)) {
  4582. if (!th->rst) {
  4583. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSESTABREJECTED);
  4584. tcp_send_dupack(sk, skb);
  4585. goto discard;
  4586. }
  4587. /* Reset is accepted even if it did not pass PAWS. */
  4588. }
  4589. /* Step 1: check sequence number */
  4590. if (!tcp_sequence(tp, TCP_SKB_CB(skb)->seq, TCP_SKB_CB(skb)->end_seq)) {
  4591. /* RFC793, page 37: "In all states except SYN-SENT, all reset
  4592. * (RST) segments are validated by checking their SEQ-fields."
  4593. * And page 69: "If an incoming segment is not acceptable,
  4594. * an acknowledgment should be sent in reply (unless the RST
  4595. * bit is set, if so drop the segment and return)".
  4596. */
  4597. if (!th->rst) {
  4598. if (th->syn)
  4599. goto syn_challenge;
  4600. tcp_send_dupack(sk, skb);
  4601. }
  4602. goto discard;
  4603. }
  4604. /* Step 2: check RST bit */
  4605. if (th->rst) {
  4606. /* RFC 5961 3.2 :
  4607. * If sequence number exactly matches RCV.NXT, then
  4608. * RESET the connection
  4609. * else
  4610. * Send a challenge ACK
  4611. */
  4612. if (TCP_SKB_CB(skb)->seq == tp->rcv_nxt)
  4613. tcp_reset(sk);
  4614. else
  4615. tcp_send_challenge_ack(sk);
  4616. goto discard;
  4617. }
  4618. /* ts_recent update must be made after we are sure that the packet
  4619. * is in window.
  4620. */
  4621. tcp_replace_ts_recent(tp, TCP_SKB_CB(skb)->seq);
  4622. /* step 3: check security and precedence [ignored] */
  4623. /* step 4: Check for a SYN
  4624. * RFC 5691 4.2 : Send a challenge ack
  4625. */
  4626. if (th->syn) {
  4627. syn_challenge:
  4628. if (syn_inerr)
  4629. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
  4630. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPSYNCHALLENGE);
  4631. tcp_send_challenge_ack(sk);
  4632. goto discard;
  4633. }
  4634. return true;
  4635. discard:
  4636. __kfree_skb(skb);
  4637. return false;
  4638. }
  4639. /*
  4640. * TCP receive function for the ESTABLISHED state.
  4641. *
  4642. * It is split into a fast path and a slow path. The fast path is
  4643. * disabled when:
  4644. * - A zero window was announced from us - zero window probing
  4645. * is only handled properly in the slow path.
  4646. * - Out of order segments arrived.
  4647. * - Urgent data is expected.
  4648. * - There is no buffer space left
  4649. * - Unexpected TCP flags/window values/header lengths are received
  4650. * (detected by checking the TCP header against pred_flags)
  4651. * - Data is sent in both directions. Fast path only supports pure senders
  4652. * or pure receivers (this means either the sequence number or the ack
  4653. * value must stay constant)
  4654. * - Unexpected TCP option.
  4655. *
  4656. * When these conditions are not satisfied it drops into a standard
  4657. * receive procedure patterned after RFC793 to handle all cases.
  4658. * The first three cases are guaranteed by proper pred_flags setting,
  4659. * the rest is checked inline. Fast processing is turned on in
  4660. * tcp_data_queue when everything is OK.
  4661. */
  4662. int tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
  4663. const struct tcphdr *th, unsigned int len)
  4664. {
  4665. struct tcp_sock *tp = tcp_sk(sk);
  4666. /*
  4667. * Header prediction.
  4668. * The code loosely follows the one in the famous
  4669. * "30 instruction TCP receive" Van Jacobson mail.
  4670. *
  4671. * Van's trick is to deposit buffers into socket queue
  4672. * on a device interrupt, to call tcp_recv function
  4673. * on the receive process context and checksum and copy
  4674. * the buffer to user space. smart...
  4675. *
  4676. * Our current scheme is not silly either but we take the
  4677. * extra cost of the net_bh soft interrupt processing...
  4678. * We do checksum and copy also but from device to kernel.
  4679. */
  4680. tp->rx_opt.saw_tstamp = 0;
  4681. /* pred_flags is 0xS?10 << 16 + snd_wnd
  4682. * if header_prediction is to be made
  4683. * 'S' will always be tp->tcp_header_len >> 2
  4684. * '?' will be 0 for the fast path, otherwise pred_flags is 0 to
  4685. * turn it off (when there are holes in the receive
  4686. * space for instance)
  4687. * PSH flag is ignored.
  4688. */
  4689. if ((tcp_flag_word(th) & TCP_HP_BITS) == tp->pred_flags &&
  4690. TCP_SKB_CB(skb)->seq == tp->rcv_nxt &&
  4691. !after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt)) {
  4692. int tcp_header_len = tp->tcp_header_len;
  4693. /* Timestamp header prediction: tcp_header_len
  4694. * is automatically equal to th->doff*4 due to pred_flags
  4695. * match.
  4696. */
  4697. /* Check timestamp */
  4698. if (tcp_header_len == sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) {
  4699. /* No? Slow path! */
  4700. if (!tcp_parse_aligned_timestamp(tp, th))
  4701. goto slow_path;
  4702. /* If PAWS failed, check it more carefully in slow path */
  4703. if ((s32)(tp->rx_opt.rcv_tsval - tp->rx_opt.ts_recent) < 0)
  4704. goto slow_path;
  4705. /* DO NOT update ts_recent here, if checksum fails
  4706. * and timestamp was corrupted part, it will result
  4707. * in a hung connection since we will drop all
  4708. * future packets due to the PAWS test.
  4709. */
  4710. }
  4711. if (len <= tcp_header_len) {
  4712. /* Bulk data transfer: sender */
  4713. if (len == tcp_header_len) {
  4714. /* Predicted packet is in window by definition.
  4715. * seq == rcv_nxt and rcv_wup <= rcv_nxt.
  4716. * Hence, check seq<=rcv_wup reduces to:
  4717. */
  4718. if (tcp_header_len ==
  4719. (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
  4720. tp->rcv_nxt == tp->rcv_wup)
  4721. tcp_store_ts_recent(tp);
  4722. /* We know that such packets are checksummed
  4723. * on entry.
  4724. */
  4725. tcp_ack(sk, skb, 0);
  4726. __kfree_skb(skb);
  4727. tcp_data_snd_check(sk);
  4728. return 0;
  4729. } else { /* Header too small */
  4730. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
  4731. goto discard;
  4732. }
  4733. } else {
  4734. int eaten = 0;
  4735. int copied_early = 0;
  4736. bool fragstolen = false;
  4737. if (tp->copied_seq == tp->rcv_nxt &&
  4738. len - tcp_header_len <= tp->ucopy.len) {
  4739. #ifdef CONFIG_NET_DMA
  4740. if (tp->ucopy.task == current &&
  4741. sock_owned_by_user(sk) &&
  4742. tcp_dma_try_early_copy(sk, skb, tcp_header_len)) {
  4743. copied_early = 1;
  4744. eaten = 1;
  4745. }
  4746. #endif
  4747. if (tp->ucopy.task == current &&
  4748. sock_owned_by_user(sk) && !copied_early) {
  4749. __set_current_state(TASK_RUNNING);
  4750. if (!tcp_copy_to_iovec(sk, skb, tcp_header_len))
  4751. eaten = 1;
  4752. }
  4753. if (eaten) {
  4754. /* Predicted packet is in window by definition.
  4755. * seq == rcv_nxt and rcv_wup <= rcv_nxt.
  4756. * Hence, check seq<=rcv_wup reduces to:
  4757. */
  4758. if (tcp_header_len ==
  4759. (sizeof(struct tcphdr) +
  4760. TCPOLEN_TSTAMP_ALIGNED) &&
  4761. tp->rcv_nxt == tp->rcv_wup)
  4762. tcp_store_ts_recent(tp);
  4763. tcp_rcv_rtt_measure_ts(sk, skb);
  4764. __skb_pull(skb, tcp_header_len);
  4765. tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  4766. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITSTOUSER);
  4767. }
  4768. if (copied_early)
  4769. tcp_cleanup_rbuf(sk, skb->len);
  4770. }
  4771. if (!eaten) {
  4772. if (tcp_checksum_complete_user(sk, skb))
  4773. goto csum_error;
  4774. /* Predicted packet is in window by definition.
  4775. * seq == rcv_nxt and rcv_wup <= rcv_nxt.
  4776. * Hence, check seq<=rcv_wup reduces to:
  4777. */
  4778. if (tcp_header_len ==
  4779. (sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED) &&
  4780. tp->rcv_nxt == tp->rcv_wup)
  4781. tcp_store_ts_recent(tp);
  4782. tcp_rcv_rtt_measure_ts(sk, skb);
  4783. if ((int)skb->truesize > sk->sk_forward_alloc)
  4784. goto step5;
  4785. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPHPHITS);
  4786. /* Bulk data transfer: receiver */
  4787. eaten = tcp_queue_rcv(sk, skb, tcp_header_len,
  4788. &fragstolen);
  4789. }
  4790. tcp_event_data_recv(sk, skb);
  4791. if (TCP_SKB_CB(skb)->ack_seq != tp->snd_una) {
  4792. /* Well, only one small jumplet in fast path... */
  4793. tcp_ack(sk, skb, FLAG_DATA);
  4794. tcp_data_snd_check(sk);
  4795. if (!inet_csk_ack_scheduled(sk))
  4796. goto no_ack;
  4797. }
  4798. if (!copied_early || tp->rcv_nxt != tp->rcv_wup)
  4799. __tcp_ack_snd_check(sk, 0);
  4800. no_ack:
  4801. #ifdef CONFIG_NET_DMA
  4802. if (copied_early)
  4803. __skb_queue_tail(&sk->sk_async_wait_queue, skb);
  4804. else
  4805. #endif
  4806. if (eaten)
  4807. kfree_skb_partial(skb, fragstolen);
  4808. else
  4809. sk->sk_data_ready(sk, 0);
  4810. return 0;
  4811. }
  4812. }
  4813. slow_path:
  4814. if (len < (th->doff << 2) || tcp_checksum_complete_user(sk, skb))
  4815. goto csum_error;
  4816. /*
  4817. * Standard slow path.
  4818. */
  4819. if (!tcp_validate_incoming(sk, skb, th, 1))
  4820. return 0;
  4821. step5:
  4822. if (th->ack && tcp_ack(sk, skb, FLAG_SLOWPATH) < 0)
  4823. goto discard;
  4824. tcp_rcv_rtt_measure_ts(sk, skb);
  4825. /* Process urgent data. */
  4826. tcp_urg(sk, skb, th);
  4827. /* step 7: process the segment text */
  4828. tcp_data_queue(sk, skb);
  4829. tcp_data_snd_check(sk);
  4830. tcp_ack_snd_check(sk);
  4831. return 0;
  4832. csum_error:
  4833. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
  4834. discard:
  4835. __kfree_skb(skb);
  4836. return 0;
  4837. }
  4838. EXPORT_SYMBOL(tcp_rcv_established);
  4839. void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
  4840. {
  4841. struct tcp_sock *tp = tcp_sk(sk);
  4842. struct inet_connection_sock *icsk = inet_csk(sk);
  4843. tcp_set_state(sk, TCP_ESTABLISHED);
  4844. if (skb != NULL) {
  4845. inet_sk_rx_dst_set(sk, skb);
  4846. security_inet_conn_established(sk, skb);
  4847. }
  4848. /* Make sure socket is routed, for correct metrics. */
  4849. icsk->icsk_af_ops->rebuild_header(sk);
  4850. tcp_init_metrics(sk);
  4851. tcp_init_congestion_control(sk);
  4852. /* Prevent spurious tcp_cwnd_restart() on first data
  4853. * packet.
  4854. */
  4855. tp->lsndtime = tcp_time_stamp;
  4856. tcp_init_buffer_space(sk);
  4857. if (sock_flag(sk, SOCK_KEEPOPEN))
  4858. inet_csk_reset_keepalive_timer(sk, keepalive_time_when(tp));
  4859. if (!tp->rx_opt.snd_wscale)
  4860. __tcp_fast_path_on(tp, tp->snd_wnd);
  4861. else
  4862. tp->pred_flags = 0;
  4863. if (!sock_flag(sk, SOCK_DEAD)) {
  4864. sk->sk_state_change(sk);
  4865. sk_wake_async(sk, SOCK_WAKE_IO, POLL_OUT);
  4866. }
  4867. }
  4868. static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
  4869. struct tcp_fastopen_cookie *cookie)
  4870. {
  4871. struct tcp_sock *tp = tcp_sk(sk);
  4872. struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL;
  4873. u16 mss = tp->rx_opt.mss_clamp;
  4874. bool syn_drop;
  4875. if (mss == tp->rx_opt.user_mss) {
  4876. struct tcp_options_received opt;
  4877. const u8 *hash_location;
  4878. /* Get original SYNACK MSS value if user MSS sets mss_clamp */
  4879. tcp_clear_options(&opt);
  4880. opt.user_mss = opt.mss_clamp = 0;
  4881. tcp_parse_options(synack, &opt, &hash_location, 0, NULL);
  4882. mss = opt.mss_clamp;
  4883. }
  4884. if (!tp->syn_fastopen) /* Ignore an unsolicited cookie */
  4885. cookie->len = -1;
  4886. /* The SYN-ACK neither has cookie nor acknowledges the data. Presumably
  4887. * the remote receives only the retransmitted (regular) SYNs: either
  4888. * the original SYN-data or the corresponding SYN-ACK is lost.
  4889. */
  4890. syn_drop = (cookie->len <= 0 && data &&
  4891. inet_csk(sk)->icsk_retransmits);
  4892. tcp_fastopen_cache_set(sk, mss, cookie, syn_drop);
  4893. if (data) { /* Retransmit unacked data in SYN */
  4894. tcp_retransmit_skb(sk, data);
  4895. tcp_rearm_rto(sk);
  4896. return true;
  4897. }
  4898. return false;
  4899. }
  4900. static int tcp_rcv_synsent_state_process(struct sock *sk, struct sk_buff *skb,
  4901. const struct tcphdr *th, unsigned int len)
  4902. {
  4903. const u8 *hash_location;
  4904. struct inet_connection_sock *icsk = inet_csk(sk);
  4905. struct tcp_sock *tp = tcp_sk(sk);
  4906. struct tcp_cookie_values *cvp = tp->cookie_values;
  4907. struct tcp_fastopen_cookie foc = { .len = -1 };
  4908. int saved_clamp = tp->rx_opt.mss_clamp;
  4909. tcp_parse_options(skb, &tp->rx_opt, &hash_location, 0, &foc);
  4910. if (th->ack) {
  4911. /* rfc793:
  4912. * "If the state is SYN-SENT then
  4913. * first check the ACK bit
  4914. * If the ACK bit is set
  4915. * If SEG.ACK =< ISS, or SEG.ACK > SND.NXT, send
  4916. * a reset (unless the RST bit is set, if so drop
  4917. * the segment and return)"
  4918. */
  4919. if (!after(TCP_SKB_CB(skb)->ack_seq, tp->snd_una) ||
  4920. after(TCP_SKB_CB(skb)->ack_seq, tp->snd_nxt))
  4921. goto reset_and_undo;
  4922. if (tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
  4923. !between(tp->rx_opt.rcv_tsecr, tp->retrans_stamp,
  4924. tcp_time_stamp)) {
  4925. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSACTIVEREJECTED);
  4926. goto reset_and_undo;
  4927. }
  4928. /* Now ACK is acceptable.
  4929. *
  4930. * "If the RST bit is set
  4931. * If the ACK was acceptable then signal the user "error:
  4932. * connection reset", drop the segment, enter CLOSED state,
  4933. * delete TCB, and return."
  4934. */
  4935. if (th->rst) {
  4936. tcp_reset(sk);
  4937. goto discard;
  4938. }
  4939. /* rfc793:
  4940. * "fifth, if neither of the SYN or RST bits is set then
  4941. * drop the segment and return."
  4942. *
  4943. * See note below!
  4944. * --ANK(990513)
  4945. */
  4946. if (!th->syn)
  4947. goto discard_and_undo;
  4948. /* rfc793:
  4949. * "If the SYN bit is on ...
  4950. * are acceptable then ...
  4951. * (our SYN has been ACKed), change the connection
  4952. * state to ESTABLISHED..."
  4953. */
  4954. TCP_ECN_rcv_synack(tp, th);
  4955. tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
  4956. tcp_ack(sk, skb, FLAG_SLOWPATH);
  4957. /* Ok.. it's good. Set up sequence numbers and
  4958. * move to established.
  4959. */
  4960. tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
  4961. tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
  4962. /* RFC1323: The window in SYN & SYN/ACK segments is
  4963. * never scaled.
  4964. */
  4965. tp->snd_wnd = ntohs(th->window);
  4966. tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
  4967. if (!tp->rx_opt.wscale_ok) {
  4968. tp->rx_opt.snd_wscale = tp->rx_opt.rcv_wscale = 0;
  4969. tp->window_clamp = min(tp->window_clamp, 65535U);
  4970. }
  4971. if (tp->rx_opt.saw_tstamp) {
  4972. tp->rx_opt.tstamp_ok = 1;
  4973. tp->tcp_header_len =
  4974. sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  4975. tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
  4976. tcp_store_ts_recent(tp);
  4977. } else {
  4978. tp->tcp_header_len = sizeof(struct tcphdr);
  4979. }
  4980. if (tcp_is_sack(tp) && sysctl_tcp_fack)
  4981. tcp_enable_fack(tp);
  4982. tcp_mtup_init(sk);
  4983. tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  4984. tcp_initialize_rcv_mss(sk);
  4985. /* Remember, tcp_poll() does not lock socket!
  4986. * Change state from SYN-SENT only after copied_seq
  4987. * is initialized. */
  4988. tp->copied_seq = tp->rcv_nxt;
  4989. if (cvp != NULL &&
  4990. cvp->cookie_pair_size > 0 &&
  4991. tp->rx_opt.cookie_plus > 0) {
  4992. int cookie_size = tp->rx_opt.cookie_plus
  4993. - TCPOLEN_COOKIE_BASE;
  4994. int cookie_pair_size = cookie_size
  4995. + cvp->cookie_desired;
  4996. /* A cookie extension option was sent and returned.
  4997. * Note that each incoming SYNACK replaces the
  4998. * Responder cookie. The initial exchange is most
  4999. * fragile, as protection against spoofing relies
  5000. * entirely upon the sequence and timestamp (above).
  5001. * This replacement strategy allows the correct pair to
  5002. * pass through, while any others will be filtered via
  5003. * Responder verification later.
  5004. */
  5005. if (sizeof(cvp->cookie_pair) >= cookie_pair_size) {
  5006. memcpy(&cvp->cookie_pair[cvp->cookie_desired],
  5007. hash_location, cookie_size);
  5008. cvp->cookie_pair_size = cookie_pair_size;
  5009. }
  5010. }
  5011. smp_mb();
  5012. tcp_finish_connect(sk, skb);
  5013. if ((tp->syn_fastopen || tp->syn_data) &&
  5014. tcp_rcv_fastopen_synack(sk, skb, &foc))
  5015. return -1;
  5016. if (sk->sk_write_pending ||
  5017. icsk->icsk_accept_queue.rskq_defer_accept ||
  5018. icsk->icsk_ack.pingpong) {
  5019. /* Save one ACK. Data will be ready after
  5020. * several ticks, if write_pending is set.
  5021. *
  5022. * It may be deleted, but with this feature tcpdumps
  5023. * look so _wonderfully_ clever, that I was not able
  5024. * to stand against the temptation 8) --ANK
  5025. */
  5026. inet_csk_schedule_ack(sk);
  5027. icsk->icsk_ack.lrcvtime = tcp_time_stamp;
  5028. tcp_enter_quickack_mode(sk);
  5029. inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
  5030. TCP_DELACK_MAX, TCP_RTO_MAX);
  5031. discard:
  5032. __kfree_skb(skb);
  5033. return 0;
  5034. } else {
  5035. tcp_send_ack(sk);
  5036. }
  5037. return -1;
  5038. }
  5039. /* No ACK in the segment */
  5040. if (th->rst) {
  5041. /* rfc793:
  5042. * "If the RST bit is set
  5043. *
  5044. * Otherwise (no ACK) drop the segment and return."
  5045. */
  5046. goto discard_and_undo;
  5047. }
  5048. /* PAWS check. */
  5049. if (tp->rx_opt.ts_recent_stamp && tp->rx_opt.saw_tstamp &&
  5050. tcp_paws_reject(&tp->rx_opt, 0))
  5051. goto discard_and_undo;
  5052. if (th->syn) {
  5053. /* We see SYN without ACK. It is attempt of
  5054. * simultaneous connect with crossed SYNs.
  5055. * Particularly, it can be connect to self.
  5056. */
  5057. tcp_set_state(sk, TCP_SYN_RECV);
  5058. if (tp->rx_opt.saw_tstamp) {
  5059. tp->rx_opt.tstamp_ok = 1;
  5060. tcp_store_ts_recent(tp);
  5061. tp->tcp_header_len =
  5062. sizeof(struct tcphdr) + TCPOLEN_TSTAMP_ALIGNED;
  5063. } else {
  5064. tp->tcp_header_len = sizeof(struct tcphdr);
  5065. }
  5066. tp->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
  5067. tp->rcv_wup = TCP_SKB_CB(skb)->seq + 1;
  5068. /* RFC1323: The window in SYN & SYN/ACK segments is
  5069. * never scaled.
  5070. */
  5071. tp->snd_wnd = ntohs(th->window);
  5072. tp->snd_wl1 = TCP_SKB_CB(skb)->seq;
  5073. tp->max_window = tp->snd_wnd;
  5074. TCP_ECN_rcv_syn(tp, th);
  5075. tcp_mtup_init(sk);
  5076. tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
  5077. tcp_initialize_rcv_mss(sk);
  5078. tcp_send_synack(sk);
  5079. #if 0
  5080. /* Note, we could accept data and URG from this segment.
  5081. * There are no obstacles to make this.
  5082. *
  5083. * However, if we ignore data in ACKless segments sometimes,
  5084. * we have no reasons to accept it sometimes.
  5085. * Also, seems the code doing it in step6 of tcp_rcv_state_process
  5086. * is not flawless. So, discard packet for sanity.
  5087. * Uncomment this return to process the data.
  5088. */
  5089. return -1;
  5090. #else
  5091. goto discard;
  5092. #endif
  5093. }
  5094. /* "fifth, if neither of the SYN or RST bits is set then
  5095. * drop the segment and return."
  5096. */
  5097. discard_and_undo:
  5098. tcp_clear_options(&tp->rx_opt);
  5099. tp->rx_opt.mss_clamp = saved_clamp;
  5100. goto discard;
  5101. reset_and_undo:
  5102. tcp_clear_options(&tp->rx_opt);
  5103. tp->rx_opt.mss_clamp = saved_clamp;
  5104. return 1;
  5105. }
  5106. /*
  5107. * This function implements the receiving procedure of RFC 793 for
  5108. * all states except ESTABLISHED and TIME_WAIT.
  5109. * It's called from both tcp_v4_rcv and tcp_v6_rcv and should be
  5110. * address independent.
  5111. */
  5112. int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
  5113. const struct tcphdr *th, unsigned int len)
  5114. {
  5115. struct tcp_sock *tp = tcp_sk(sk);
  5116. struct inet_connection_sock *icsk = inet_csk(sk);
  5117. int queued = 0;
  5118. tp->rx_opt.saw_tstamp = 0;
  5119. switch (sk->sk_state) {
  5120. case TCP_CLOSE:
  5121. goto discard;
  5122. case TCP_LISTEN:
  5123. if (th->ack)
  5124. return 1;
  5125. if (th->rst)
  5126. goto discard;
  5127. if (th->syn) {
  5128. if (th->fin)
  5129. goto discard;
  5130. if (icsk->icsk_af_ops->conn_request(sk, skb) < 0)
  5131. return 1;
  5132. /* Now we have several options: In theory there is
  5133. * nothing else in the frame. KA9Q has an option to
  5134. * send data with the syn, BSD accepts data with the
  5135. * syn up to the [to be] advertised window and
  5136. * Solaris 2.1 gives you a protocol error. For now
  5137. * we just ignore it, that fits the spec precisely
  5138. * and avoids incompatibilities. It would be nice in
  5139. * future to drop through and process the data.
  5140. *
  5141. * Now that TTCP is starting to be used we ought to
  5142. * queue this data.
  5143. * But, this leaves one open to an easy denial of
  5144. * service attack, and SYN cookies can't defend
  5145. * against this problem. So, we drop the data
  5146. * in the interest of security over speed unless
  5147. * it's still in use.
  5148. */
  5149. kfree_skb(skb);
  5150. return 0;
  5151. }
  5152. goto discard;
  5153. case TCP_SYN_SENT:
  5154. queued = tcp_rcv_synsent_state_process(sk, skb, th, len);
  5155. if (queued >= 0)
  5156. return queued;
  5157. /* Do step6 onward by hand. */
  5158. tcp_urg(sk, skb, th);
  5159. __kfree_skb(skb);
  5160. tcp_data_snd_check(sk);
  5161. return 0;
  5162. }
  5163. if (!tcp_validate_incoming(sk, skb, th, 0))
  5164. return 0;
  5165. /* step 5: check the ACK field */
  5166. if (th->ack) {
  5167. int acceptable = tcp_ack(sk, skb, FLAG_SLOWPATH) > 0;
  5168. switch (sk->sk_state) {
  5169. case TCP_SYN_RECV:
  5170. if (acceptable) {
  5171. tp->copied_seq = tp->rcv_nxt;
  5172. smp_mb();
  5173. tcp_set_state(sk, TCP_ESTABLISHED);
  5174. sk->sk_state_change(sk);
  5175. /* Note, that this wakeup is only for marginal
  5176. * crossed SYN case. Passively open sockets
  5177. * are not waked up, because sk->sk_sleep ==
  5178. * NULL and sk->sk_socket == NULL.
  5179. */
  5180. if (sk->sk_socket)
  5181. sk_wake_async(sk,
  5182. SOCK_WAKE_IO, POLL_OUT);
  5183. tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
  5184. tp->snd_wnd = ntohs(th->window) <<
  5185. tp->rx_opt.snd_wscale;
  5186. tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
  5187. if (tp->rx_opt.tstamp_ok)
  5188. tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
  5189. /* Make sure socket is routed, for
  5190. * correct metrics.
  5191. */
  5192. icsk->icsk_af_ops->rebuild_header(sk);
  5193. tcp_init_metrics(sk);
  5194. tcp_init_congestion_control(sk);
  5195. /* Prevent spurious tcp_cwnd_restart() on
  5196. * first data packet.
  5197. */
  5198. tp->lsndtime = tcp_time_stamp;
  5199. tcp_mtup_init(sk);
  5200. tcp_initialize_rcv_mss(sk);
  5201. tcp_init_buffer_space(sk);
  5202. tcp_fast_path_on(tp);
  5203. } else {
  5204. return 1;
  5205. }
  5206. break;
  5207. case TCP_FIN_WAIT1:
  5208. if (tp->snd_una == tp->write_seq) {
  5209. struct dst_entry *dst;
  5210. tcp_set_state(sk, TCP_FIN_WAIT2);
  5211. sk->sk_shutdown |= SEND_SHUTDOWN;
  5212. dst = __sk_dst_get(sk);
  5213. if (dst)
  5214. dst_confirm(dst);
  5215. if (!sock_flag(sk, SOCK_DEAD))
  5216. /* Wake up lingering close() */
  5217. sk->sk_state_change(sk);
  5218. else {
  5219. int tmo;
  5220. if (tp->linger2 < 0 ||
  5221. (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
  5222. after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt))) {
  5223. tcp_done(sk);
  5224. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
  5225. return 1;
  5226. }
  5227. tmo = tcp_fin_time(sk);
  5228. if (tmo > TCP_TIMEWAIT_LEN) {
  5229. inet_csk_reset_keepalive_timer(sk, tmo - TCP_TIMEWAIT_LEN);
  5230. } else if (th->fin || sock_owned_by_user(sk)) {
  5231. /* Bad case. We could lose such FIN otherwise.
  5232. * It is not a big problem, but it looks confusing
  5233. * and not so rare event. We still can lose it now,
  5234. * if it spins in bh_lock_sock(), but it is really
  5235. * marginal case.
  5236. */
  5237. inet_csk_reset_keepalive_timer(sk, tmo);
  5238. } else {
  5239. tcp_time_wait(sk, TCP_FIN_WAIT2, tmo);
  5240. goto discard;
  5241. }
  5242. }
  5243. }
  5244. break;
  5245. case TCP_CLOSING:
  5246. if (tp->snd_una == tp->write_seq) {
  5247. tcp_time_wait(sk, TCP_TIME_WAIT, 0);
  5248. goto discard;
  5249. }
  5250. break;
  5251. case TCP_LAST_ACK:
  5252. if (tp->snd_una == tp->write_seq) {
  5253. tcp_update_metrics(sk);
  5254. tcp_done(sk);
  5255. goto discard;
  5256. }
  5257. break;
  5258. }
  5259. } else
  5260. goto discard;
  5261. /* step 6: check the URG bit */
  5262. tcp_urg(sk, skb, th);
  5263. /* step 7: process the segment text */
  5264. switch (sk->sk_state) {
  5265. case TCP_CLOSE_WAIT:
  5266. case TCP_CLOSING:
  5267. case TCP_LAST_ACK:
  5268. if (!before(TCP_SKB_CB(skb)->seq, tp->rcv_nxt))
  5269. break;
  5270. case TCP_FIN_WAIT1:
  5271. case TCP_FIN_WAIT2:
  5272. /* RFC 793 says to queue data in these states,
  5273. * RFC 1122 says we MUST send a reset.
  5274. * BSD 4.4 also does reset.
  5275. */
  5276. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  5277. if (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq &&
  5278. after(TCP_SKB_CB(skb)->end_seq - th->fin, tp->rcv_nxt)) {
  5279. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPABORTONDATA);
  5280. tcp_reset(sk);
  5281. return 1;
  5282. }
  5283. }
  5284. /* Fall through */
  5285. case TCP_ESTABLISHED:
  5286. tcp_data_queue(sk, skb);
  5287. queued = 1;
  5288. break;
  5289. }
  5290. /* tcp_data could move socket to TIME-WAIT */
  5291. if (sk->sk_state != TCP_CLOSE) {
  5292. tcp_data_snd_check(sk);
  5293. tcp_ack_snd_check(sk);
  5294. }
  5295. if (!queued) {
  5296. discard:
  5297. __kfree_skb(skb);
  5298. }
  5299. return 0;
  5300. }
  5301. EXPORT_SYMBOL(tcp_rcv_state_process);