skbuff.h 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122
  1. /*
  2. * Definitions for the 'struct sk_buff' memory handlers.
  3. *
  4. * Authors:
  5. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  6. * Florian La Roche, <rzsfl@rz.uni-sb.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #ifndef _LINUX_SKBUFF_H
  14. #define _LINUX_SKBUFF_H
  15. #include <linux/kernel.h>
  16. #include <linux/kmemcheck.h>
  17. #include <linux/compiler.h>
  18. #include <linux/time.h>
  19. #include <linux/bug.h>
  20. #include <linux/cache.h>
  21. #include <linux/rbtree.h>
  22. #include <linux/socket.h>
  23. #include <linux/refcount.h>
  24. #include <linux/atomic.h>
  25. #include <asm/types.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/net.h>
  28. #include <linux/textsearch.h>
  29. #include <net/checksum.h>
  30. #include <linux/rcupdate.h>
  31. #include <linux/hrtimer.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/netdev_features.h>
  34. #include <linux/sched.h>
  35. #include <linux/sched/clock.h>
  36. #include <net/flow_dissector.h>
  37. #include <linux/splice.h>
  38. #include <linux/in6.h>
  39. #include <linux/if_packet.h>
  40. #include <net/flow.h>
  41. /* The interface for checksum offload between the stack and networking drivers
  42. * is as follows...
  43. *
  44. * A. IP checksum related features
  45. *
  46. * Drivers advertise checksum offload capabilities in the features of a device.
  47. * From the stack's point of view these are capabilities offered by the driver,
  48. * a driver typically only advertises features that it is capable of offloading
  49. * to its device.
  50. *
  51. * The checksum related features are:
  52. *
  53. * NETIF_F_HW_CSUM - The driver (or its device) is able to compute one
  54. * IP (one's complement) checksum for any combination
  55. * of protocols or protocol layering. The checksum is
  56. * computed and set in a packet per the CHECKSUM_PARTIAL
  57. * interface (see below).
  58. *
  59. * NETIF_F_IP_CSUM - Driver (device) is only able to checksum plain
  60. * TCP or UDP packets over IPv4. These are specifically
  61. * unencapsulated packets of the form IPv4|TCP or
  62. * IPv4|UDP where the Protocol field in the IPv4 header
  63. * is TCP or UDP. The IPv4 header may contain IP options
  64. * This feature cannot be set in features for a device
  65. * with NETIF_F_HW_CSUM also set. This feature is being
  66. * DEPRECATED (see below).
  67. *
  68. * NETIF_F_IPV6_CSUM - Driver (device) is only able to checksum plain
  69. * TCP or UDP packets over IPv6. These are specifically
  70. * unencapsulated packets of the form IPv6|TCP or
  71. * IPv4|UDP where the Next Header field in the IPv6
  72. * header is either TCP or UDP. IPv6 extension headers
  73. * are not supported with this feature. This feature
  74. * cannot be set in features for a device with
  75. * NETIF_F_HW_CSUM also set. This feature is being
  76. * DEPRECATED (see below).
  77. *
  78. * NETIF_F_RXCSUM - Driver (device) performs receive checksum offload.
  79. * This flag is used only used to disable the RX checksum
  80. * feature for a device. The stack will accept receive
  81. * checksum indication in packets received on a device
  82. * regardless of whether NETIF_F_RXCSUM is set.
  83. *
  84. * B. Checksumming of received packets by device. Indication of checksum
  85. * verification is in set skb->ip_summed. Possible values are:
  86. *
  87. * CHECKSUM_NONE:
  88. *
  89. * Device did not checksum this packet e.g. due to lack of capabilities.
  90. * The packet contains full (though not verified) checksum in packet but
  91. * not in skb->csum. Thus, skb->csum is undefined in this case.
  92. *
  93. * CHECKSUM_UNNECESSARY:
  94. *
  95. * The hardware you're dealing with doesn't calculate the full checksum
  96. * (as in CHECKSUM_COMPLETE), but it does parse headers and verify checksums
  97. * for specific protocols. For such packets it will set CHECKSUM_UNNECESSARY
  98. * if their checksums are okay. skb->csum is still undefined in this case
  99. * though. A driver or device must never modify the checksum field in the
  100. * packet even if checksum is verified.
  101. *
  102. * CHECKSUM_UNNECESSARY is applicable to following protocols:
  103. * TCP: IPv6 and IPv4.
  104. * UDP: IPv4 and IPv6. A device may apply CHECKSUM_UNNECESSARY to a
  105. * zero UDP checksum for either IPv4 or IPv6, the networking stack
  106. * may perform further validation in this case.
  107. * GRE: only if the checksum is present in the header.
  108. * SCTP: indicates the CRC in SCTP header has been validated.
  109. * FCOE: indicates the CRC in FC frame has been validated.
  110. *
  111. * skb->csum_level indicates the number of consecutive checksums found in
  112. * the packet minus one that have been verified as CHECKSUM_UNNECESSARY.
  113. * For instance if a device receives an IPv6->UDP->GRE->IPv4->TCP packet
  114. * and a device is able to verify the checksums for UDP (possibly zero),
  115. * GRE (checksum flag is set), and TCP-- skb->csum_level would be set to
  116. * two. If the device were only able to verify the UDP checksum and not
  117. * GRE, either because it doesn't support GRE checksum of because GRE
  118. * checksum is bad, skb->csum_level would be set to zero (TCP checksum is
  119. * not considered in this case).
  120. *
  121. * CHECKSUM_COMPLETE:
  122. *
  123. * This is the most generic way. The device supplied checksum of the _whole_
  124. * packet as seen by netif_rx() and fills out in skb->csum. Meaning, the
  125. * hardware doesn't need to parse L3/L4 headers to implement this.
  126. *
  127. * Notes:
  128. * - Even if device supports only some protocols, but is able to produce
  129. * skb->csum, it MUST use CHECKSUM_COMPLETE, not CHECKSUM_UNNECESSARY.
  130. * - CHECKSUM_COMPLETE is not applicable to SCTP and FCoE protocols.
  131. *
  132. * CHECKSUM_PARTIAL:
  133. *
  134. * A checksum is set up to be offloaded to a device as described in the
  135. * output description for CHECKSUM_PARTIAL. This may occur on a packet
  136. * received directly from another Linux OS, e.g., a virtualized Linux kernel
  137. * on the same host, or it may be set in the input path in GRO or remote
  138. * checksum offload. For the purposes of checksum verification, the checksum
  139. * referred to by skb->csum_start + skb->csum_offset and any preceding
  140. * checksums in the packet are considered verified. Any checksums in the
  141. * packet that are after the checksum being offloaded are not considered to
  142. * be verified.
  143. *
  144. * C. Checksumming on transmit for non-GSO. The stack requests checksum offload
  145. * in the skb->ip_summed for a packet. Values are:
  146. *
  147. * CHECKSUM_PARTIAL:
  148. *
  149. * The driver is required to checksum the packet as seen by hard_start_xmit()
  150. * from skb->csum_start up to the end, and to record/write the checksum at
  151. * offset skb->csum_start + skb->csum_offset. A driver may verify that the
  152. * csum_start and csum_offset values are valid values given the length and
  153. * offset of the packet, however they should not attempt to validate that the
  154. * checksum refers to a legitimate transport layer checksum-- it is the
  155. * purview of the stack to validate that csum_start and csum_offset are set
  156. * correctly.
  157. *
  158. * When the stack requests checksum offload for a packet, the driver MUST
  159. * ensure that the checksum is set correctly. A driver can either offload the
  160. * checksum calculation to the device, or call skb_checksum_help (in the case
  161. * that the device does not support offload for a particular checksum).
  162. *
  163. * NETIF_F_IP_CSUM and NETIF_F_IPV6_CSUM are being deprecated in favor of
  164. * NETIF_F_HW_CSUM. New devices should use NETIF_F_HW_CSUM to indicate
  165. * checksum offload capability.
  166. * skb_csum_hwoffload_help() can be called to resolve CHECKSUM_PARTIAL based
  167. * on network device checksumming capabilities: if a packet does not match
  168. * them, skb_checksum_help or skb_crc32c_help (depending on the value of
  169. * csum_not_inet, see item D.) is called to resolve the checksum.
  170. *
  171. * CHECKSUM_NONE:
  172. *
  173. * The skb was already checksummed by the protocol, or a checksum is not
  174. * required.
  175. *
  176. * CHECKSUM_UNNECESSARY:
  177. *
  178. * This has the same meaning on as CHECKSUM_NONE for checksum offload on
  179. * output.
  180. *
  181. * CHECKSUM_COMPLETE:
  182. * Not used in checksum output. If a driver observes a packet with this value
  183. * set in skbuff, if should treat as CHECKSUM_NONE being set.
  184. *
  185. * D. Non-IP checksum (CRC) offloads
  186. *
  187. * NETIF_F_SCTP_CRC - This feature indicates that a device is capable of
  188. * offloading the SCTP CRC in a packet. To perform this offload the stack
  189. * will set set csum_start and csum_offset accordingly, set ip_summed to
  190. * CHECKSUM_PARTIAL and set csum_not_inet to 1, to provide an indication in
  191. * the skbuff that the CHECKSUM_PARTIAL refers to CRC32c.
  192. * A driver that supports both IP checksum offload and SCTP CRC32c offload
  193. * must verify which offload is configured for a packet by testing the
  194. * value of skb->csum_not_inet; skb_crc32c_csum_help is provided to resolve
  195. * CHECKSUM_PARTIAL on skbs where csum_not_inet is set to 1.
  196. *
  197. * NETIF_F_FCOE_CRC - This feature indicates that a device is capable of
  198. * offloading the FCOE CRC in a packet. To perform this offload the stack
  199. * will set ip_summed to CHECKSUM_PARTIAL and set csum_start and csum_offset
  200. * accordingly. Note the there is no indication in the skbuff that the
  201. * CHECKSUM_PARTIAL refers to an FCOE checksum, a driver that supports
  202. * both IP checksum offload and FCOE CRC offload must verify which offload
  203. * is configured for a packet presumably by inspecting packet headers.
  204. *
  205. * E. Checksumming on output with GSO.
  206. *
  207. * In the case of a GSO packet (skb_is_gso(skb) is true), checksum offload
  208. * is implied by the SKB_GSO_* flags in gso_type. Most obviously, if the
  209. * gso_type is SKB_GSO_TCPV4 or SKB_GSO_TCPV6, TCP checksum offload as
  210. * part of the GSO operation is implied. If a checksum is being offloaded
  211. * with GSO then ip_summed is CHECKSUM_PARTIAL, csum_start and csum_offset
  212. * are set to refer to the outermost checksum being offload (two offloaded
  213. * checksums are possible with UDP encapsulation).
  214. */
  215. /* Don't change this without changing skb_csum_unnecessary! */
  216. #define CHECKSUM_NONE 0
  217. #define CHECKSUM_UNNECESSARY 1
  218. #define CHECKSUM_COMPLETE 2
  219. #define CHECKSUM_PARTIAL 3
  220. /* Maximum value in skb->csum_level */
  221. #define SKB_MAX_CSUM_LEVEL 3
  222. #define SKB_DATA_ALIGN(X) ALIGN(X, SMP_CACHE_BYTES)
  223. #define SKB_WITH_OVERHEAD(X) \
  224. ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  225. #define SKB_MAX_ORDER(X, ORDER) \
  226. SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
  227. #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
  228. #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
  229. /* return minimum truesize of one skb containing X bytes of data */
  230. #define SKB_TRUESIZE(X) ((X) + \
  231. SKB_DATA_ALIGN(sizeof(struct sk_buff)) + \
  232. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  233. struct net_device;
  234. struct scatterlist;
  235. struct pipe_inode_info;
  236. struct iov_iter;
  237. struct napi_struct;
  238. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  239. struct nf_conntrack {
  240. atomic_t use;
  241. };
  242. #endif
  243. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  244. struct nf_bridge_info {
  245. refcount_t use;
  246. enum {
  247. BRNF_PROTO_UNCHANGED,
  248. BRNF_PROTO_8021Q,
  249. BRNF_PROTO_PPPOE
  250. } orig_proto:8;
  251. u8 pkt_otherhost:1;
  252. u8 in_prerouting:1;
  253. u8 bridged_dnat:1;
  254. __u16 frag_max_size;
  255. struct net_device *physindev;
  256. /* always valid & non-NULL from FORWARD on, for physdev match */
  257. struct net_device *physoutdev;
  258. union {
  259. /* prerouting: detect dnat in orig/reply direction */
  260. __be32 ipv4_daddr;
  261. struct in6_addr ipv6_daddr;
  262. /* after prerouting + nat detected: store original source
  263. * mac since neigh resolution overwrites it, only used while
  264. * skb is out in neigh layer.
  265. */
  266. char neigh_header[8];
  267. };
  268. };
  269. #endif
  270. struct sk_buff_head {
  271. /* These two members must be first. */
  272. struct sk_buff *next;
  273. struct sk_buff *prev;
  274. __u32 qlen;
  275. spinlock_t lock;
  276. };
  277. struct sk_buff;
  278. /* To allow 64K frame to be packed as single skb without frag_list we
  279. * require 64K/PAGE_SIZE pages plus 1 additional page to allow for
  280. * buffers which do not start on a page boundary.
  281. *
  282. * Since GRO uses frags we allocate at least 16 regardless of page
  283. * size.
  284. */
  285. #if (65536/PAGE_SIZE + 1) < 16
  286. #define MAX_SKB_FRAGS 16UL
  287. #else
  288. #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 1)
  289. #endif
  290. extern int sysctl_max_skb_frags;
  291. /* Set skb_shinfo(skb)->gso_size to this in case you want skb_segment to
  292. * segment using its current segmentation instead.
  293. */
  294. #define GSO_BY_FRAGS 0xFFFF
  295. typedef struct skb_frag_struct skb_frag_t;
  296. struct skb_frag_struct {
  297. struct {
  298. struct page *p;
  299. } page;
  300. #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
  301. __u32 page_offset;
  302. __u32 size;
  303. #else
  304. __u16 page_offset;
  305. __u16 size;
  306. #endif
  307. };
  308. static inline unsigned int skb_frag_size(const skb_frag_t *frag)
  309. {
  310. return frag->size;
  311. }
  312. static inline void skb_frag_size_set(skb_frag_t *frag, unsigned int size)
  313. {
  314. frag->size = size;
  315. }
  316. static inline void skb_frag_size_add(skb_frag_t *frag, int delta)
  317. {
  318. frag->size += delta;
  319. }
  320. static inline void skb_frag_size_sub(skb_frag_t *frag, int delta)
  321. {
  322. frag->size -= delta;
  323. }
  324. static inline bool skb_frag_must_loop(struct page *p)
  325. {
  326. #if defined(CONFIG_HIGHMEM)
  327. if (PageHighMem(p))
  328. return true;
  329. #endif
  330. return false;
  331. }
  332. /**
  333. * skb_frag_foreach_page - loop over pages in a fragment
  334. *
  335. * @f: skb frag to operate on
  336. * @f_off: offset from start of f->page.p
  337. * @f_len: length from f_off to loop over
  338. * @p: (temp var) current page
  339. * @p_off: (temp var) offset from start of current page,
  340. * non-zero only on first page.
  341. * @p_len: (temp var) length in current page,
  342. * < PAGE_SIZE only on first and last page.
  343. * @copied: (temp var) length so far, excluding current p_len.
  344. *
  345. * A fragment can hold a compound page, in which case per-page
  346. * operations, notably kmap_atomic, must be called for each
  347. * regular page.
  348. */
  349. #define skb_frag_foreach_page(f, f_off, f_len, p, p_off, p_len, copied) \
  350. for (p = skb_frag_page(f) + ((f_off) >> PAGE_SHIFT), \
  351. p_off = (f_off) & (PAGE_SIZE - 1), \
  352. p_len = skb_frag_must_loop(p) ? \
  353. min_t(u32, f_len, PAGE_SIZE - p_off) : f_len, \
  354. copied = 0; \
  355. copied < f_len; \
  356. copied += p_len, p++, p_off = 0, \
  357. p_len = min_t(u32, f_len - copied, PAGE_SIZE)) \
  358. #define HAVE_HW_TIME_STAMP
  359. /**
  360. * struct skb_shared_hwtstamps - hardware time stamps
  361. * @hwtstamp: hardware time stamp transformed into duration
  362. * since arbitrary point in time
  363. *
  364. * Software time stamps generated by ktime_get_real() are stored in
  365. * skb->tstamp.
  366. *
  367. * hwtstamps can only be compared against other hwtstamps from
  368. * the same device.
  369. *
  370. * This structure is attached to packets as part of the
  371. * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
  372. */
  373. struct skb_shared_hwtstamps {
  374. ktime_t hwtstamp;
  375. };
  376. /* Definitions for tx_flags in struct skb_shared_info */
  377. enum {
  378. /* generate hardware time stamp */
  379. SKBTX_HW_TSTAMP = 1 << 0,
  380. /* generate software time stamp when queueing packet to NIC */
  381. SKBTX_SW_TSTAMP = 1 << 1,
  382. /* device driver is going to provide hardware time stamp */
  383. SKBTX_IN_PROGRESS = 1 << 2,
  384. /* device driver supports TX zero-copy buffers */
  385. SKBTX_DEV_ZEROCOPY = 1 << 3,
  386. /* generate wifi status information (where possible) */
  387. SKBTX_WIFI_STATUS = 1 << 4,
  388. /* This indicates at least one fragment might be overwritten
  389. * (as in vmsplice(), sendfile() ...)
  390. * If we need to compute a TX checksum, we'll need to copy
  391. * all frags to avoid possible bad checksum
  392. */
  393. SKBTX_SHARED_FRAG = 1 << 5,
  394. /* generate software time stamp when entering packet scheduling */
  395. SKBTX_SCHED_TSTAMP = 1 << 6,
  396. };
  397. #define SKBTX_ZEROCOPY_FRAG (SKBTX_DEV_ZEROCOPY | SKBTX_SHARED_FRAG)
  398. #define SKBTX_ANY_SW_TSTAMP (SKBTX_SW_TSTAMP | \
  399. SKBTX_SCHED_TSTAMP)
  400. #define SKBTX_ANY_TSTAMP (SKBTX_HW_TSTAMP | SKBTX_ANY_SW_TSTAMP)
  401. /*
  402. * The callback notifies userspace to release buffers when skb DMA is done in
  403. * lower device, the skb last reference should be 0 when calling this.
  404. * The zerocopy_success argument is true if zero copy transmit occurred,
  405. * false on data copy or out of memory error caused by data copy attempt.
  406. * The ctx field is used to track device context.
  407. * The desc field is used to track userspace buffer index.
  408. */
  409. struct ubuf_info {
  410. void (*callback)(struct ubuf_info *, bool zerocopy_success);
  411. union {
  412. struct {
  413. unsigned long desc;
  414. void *ctx;
  415. };
  416. struct {
  417. u32 id;
  418. u16 len;
  419. u16 zerocopy:1;
  420. u32 bytelen;
  421. };
  422. };
  423. refcount_t refcnt;
  424. struct mmpin {
  425. struct user_struct *user;
  426. unsigned int num_pg;
  427. } mmp;
  428. };
  429. #define skb_uarg(SKB) ((struct ubuf_info *)(skb_shinfo(SKB)->destructor_arg))
  430. struct ubuf_info *sock_zerocopy_alloc(struct sock *sk, size_t size);
  431. struct ubuf_info *sock_zerocopy_realloc(struct sock *sk, size_t size,
  432. struct ubuf_info *uarg);
  433. static inline void sock_zerocopy_get(struct ubuf_info *uarg)
  434. {
  435. refcount_inc(&uarg->refcnt);
  436. }
  437. void sock_zerocopy_put(struct ubuf_info *uarg);
  438. void sock_zerocopy_put_abort(struct ubuf_info *uarg);
  439. void sock_zerocopy_callback(struct ubuf_info *uarg, bool success);
  440. int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
  441. struct msghdr *msg, int len,
  442. struct ubuf_info *uarg);
  443. /* This data is invariant across clones and lives at
  444. * the end of the header data, ie. at skb->end.
  445. */
  446. struct skb_shared_info {
  447. __u8 __unused;
  448. __u8 meta_len;
  449. __u8 nr_frags;
  450. __u8 tx_flags;
  451. unsigned short gso_size;
  452. /* Warning: this field is not always filled in (UFO)! */
  453. unsigned short gso_segs;
  454. struct sk_buff *frag_list;
  455. struct skb_shared_hwtstamps hwtstamps;
  456. unsigned int gso_type;
  457. u32 tskey;
  458. __be32 ip6_frag_id;
  459. /*
  460. * Warning : all fields before dataref are cleared in __alloc_skb()
  461. */
  462. atomic_t dataref;
  463. /* Intermediate layers must ensure that destructor_arg
  464. * remains valid until skb destructor */
  465. void * destructor_arg;
  466. /* must be last field, see pskb_expand_head() */
  467. skb_frag_t frags[MAX_SKB_FRAGS];
  468. };
  469. /* We divide dataref into two halves. The higher 16 bits hold references
  470. * to the payload part of skb->data. The lower 16 bits hold references to
  471. * the entire skb->data. A clone of a headerless skb holds the length of
  472. * the header in skb->hdr_len.
  473. *
  474. * All users must obey the rule that the skb->data reference count must be
  475. * greater than or equal to the payload reference count.
  476. *
  477. * Holding a reference to the payload part means that the user does not
  478. * care about modifications to the header part of skb->data.
  479. */
  480. #define SKB_DATAREF_SHIFT 16
  481. #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
  482. enum {
  483. SKB_FCLONE_UNAVAILABLE, /* skb has no fclone (from head_cache) */
  484. SKB_FCLONE_ORIG, /* orig skb (from fclone_cache) */
  485. SKB_FCLONE_CLONE, /* companion fclone skb (from fclone_cache) */
  486. };
  487. enum {
  488. SKB_GSO_TCPV4 = 1 << 0,
  489. /* This indicates the skb is from an untrusted source. */
  490. SKB_GSO_DODGY = 1 << 1,
  491. /* This indicates the tcp segment has CWR set. */
  492. SKB_GSO_TCP_ECN = 1 << 2,
  493. SKB_GSO_TCP_FIXEDID = 1 << 3,
  494. SKB_GSO_TCPV6 = 1 << 4,
  495. SKB_GSO_FCOE = 1 << 5,
  496. SKB_GSO_GRE = 1 << 6,
  497. SKB_GSO_GRE_CSUM = 1 << 7,
  498. SKB_GSO_IPXIP4 = 1 << 8,
  499. SKB_GSO_IPXIP6 = 1 << 9,
  500. SKB_GSO_UDP_TUNNEL = 1 << 10,
  501. SKB_GSO_UDP_TUNNEL_CSUM = 1 << 11,
  502. SKB_GSO_PARTIAL = 1 << 12,
  503. SKB_GSO_TUNNEL_REMCSUM = 1 << 13,
  504. SKB_GSO_SCTP = 1 << 14,
  505. SKB_GSO_ESP = 1 << 15,
  506. };
  507. #if BITS_PER_LONG > 32
  508. #define NET_SKBUFF_DATA_USES_OFFSET 1
  509. #endif
  510. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  511. typedef unsigned int sk_buff_data_t;
  512. #else
  513. typedef unsigned char *sk_buff_data_t;
  514. #endif
  515. /**
  516. * struct sk_buff - socket buffer
  517. * @next: Next buffer in list
  518. * @prev: Previous buffer in list
  519. * @tstamp: Time we arrived/left
  520. * @rbnode: RB tree node, alternative to next/prev for netem/tcp
  521. * @sk: Socket we are owned by
  522. * @dev: Device we arrived on/are leaving by
  523. * @cb: Control buffer. Free for use by every layer. Put private vars here
  524. * @_skb_refdst: destination entry (with norefcount bit)
  525. * @sp: the security path, used for xfrm
  526. * @len: Length of actual data
  527. * @data_len: Data length
  528. * @mac_len: Length of link layer header
  529. * @hdr_len: writable header length of cloned skb
  530. * @csum: Checksum (must include start/offset pair)
  531. * @csum_start: Offset from skb->head where checksumming should start
  532. * @csum_offset: Offset from csum_start where checksum should be stored
  533. * @priority: Packet queueing priority
  534. * @ignore_df: allow local fragmentation
  535. * @cloned: Head may be cloned (check refcnt to be sure)
  536. * @ip_summed: Driver fed us an IP checksum
  537. * @nohdr: Payload reference only, must not modify header
  538. * @pkt_type: Packet class
  539. * @fclone: skbuff clone status
  540. * @ipvs_property: skbuff is owned by ipvs
  541. * @tc_skip_classify: do not classify packet. set by IFB device
  542. * @tc_at_ingress: used within tc_classify to distinguish in/egress
  543. * @tc_redirected: packet was redirected by a tc action
  544. * @tc_from_ingress: if tc_redirected, tc_at_ingress at time of redirect
  545. * @peeked: this packet has been seen already, so stats have been
  546. * done for it, don't do them again
  547. * @nf_trace: netfilter packet trace flag
  548. * @protocol: Packet protocol from driver
  549. * @destructor: Destruct function
  550. * @tcp_tsorted_anchor: list structure for TCP (tp->tsorted_sent_queue)
  551. * @_nfct: Associated connection, if any (with nfctinfo bits)
  552. * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
  553. * @skb_iif: ifindex of device we arrived on
  554. * @tc_index: Traffic control index
  555. * @hash: the packet hash
  556. * @queue_mapping: Queue mapping for multiqueue devices
  557. * @xmit_more: More SKBs are pending for this queue
  558. * @ndisc_nodetype: router type (from link layer)
  559. * @ooo_okay: allow the mapping of a socket to a queue to be changed
  560. * @l4_hash: indicate hash is a canonical 4-tuple hash over transport
  561. * ports.
  562. * @sw_hash: indicates hash was computed in software stack
  563. * @wifi_acked_valid: wifi_acked was set
  564. * @wifi_acked: whether frame was acked on wifi or not
  565. * @no_fcs: Request NIC to treat last 4 bytes as Ethernet FCS
  566. * @csum_not_inet: use CRC32c to resolve CHECKSUM_PARTIAL
  567. * @dst_pending_confirm: need to confirm neighbour
  568. * @napi_id: id of the NAPI struct this skb came from
  569. * @secmark: security marking
  570. * @mark: Generic packet mark
  571. * @vlan_proto: vlan encapsulation protocol
  572. * @vlan_tci: vlan tag control information
  573. * @inner_protocol: Protocol (encapsulation)
  574. * @inner_transport_header: Inner transport layer header (encapsulation)
  575. * @inner_network_header: Network layer header (encapsulation)
  576. * @inner_mac_header: Link layer header (encapsulation)
  577. * @transport_header: Transport layer header
  578. * @network_header: Network layer header
  579. * @mac_header: Link layer header
  580. * @tail: Tail pointer
  581. * @end: End pointer
  582. * @head: Head of buffer
  583. * @data: Data head pointer
  584. * @truesize: Buffer size
  585. * @users: User count - see {datagram,tcp}.c
  586. */
  587. struct sk_buff {
  588. union {
  589. struct {
  590. /* These two members must be first. */
  591. struct sk_buff *next;
  592. struct sk_buff *prev;
  593. union {
  594. struct net_device *dev;
  595. /* Some protocols might use this space to store information,
  596. * while device pointer would be NULL.
  597. * UDP receive path is one user.
  598. */
  599. unsigned long dev_scratch;
  600. };
  601. };
  602. struct rb_node rbnode; /* used in netem & tcp stack */
  603. };
  604. struct sock *sk;
  605. union {
  606. ktime_t tstamp;
  607. u64 skb_mstamp;
  608. };
  609. /*
  610. * This is the control buffer. It is free to use for every
  611. * layer. Please put your private variables there. If you
  612. * want to keep them across layers you have to do a skb_clone()
  613. * first. This is owned by whoever has the skb queued ATM.
  614. */
  615. char cb[48] __aligned(8);
  616. union {
  617. struct {
  618. unsigned long _skb_refdst;
  619. void (*destructor)(struct sk_buff *skb);
  620. };
  621. struct list_head tcp_tsorted_anchor;
  622. };
  623. #ifdef CONFIG_XFRM
  624. struct sec_path *sp;
  625. #endif
  626. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  627. unsigned long _nfct;
  628. #endif
  629. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  630. struct nf_bridge_info *nf_bridge;
  631. #endif
  632. unsigned int len,
  633. data_len;
  634. __u16 mac_len,
  635. hdr_len;
  636. /* Following fields are _not_ copied in __copy_skb_header()
  637. * Note that queue_mapping is here mostly to fill a hole.
  638. */
  639. kmemcheck_bitfield_begin(flags1);
  640. __u16 queue_mapping;
  641. /* if you move cloned around you also must adapt those constants */
  642. #ifdef __BIG_ENDIAN_BITFIELD
  643. #define CLONED_MASK (1 << 7)
  644. #else
  645. #define CLONED_MASK 1
  646. #endif
  647. #define CLONED_OFFSET() offsetof(struct sk_buff, __cloned_offset)
  648. __u8 __cloned_offset[0];
  649. __u8 cloned:1,
  650. nohdr:1,
  651. fclone:2,
  652. peeked:1,
  653. head_frag:1,
  654. xmit_more:1,
  655. __unused:1; /* one bit hole */
  656. kmemcheck_bitfield_end(flags1);
  657. /* fields enclosed in headers_start/headers_end are copied
  658. * using a single memcpy() in __copy_skb_header()
  659. */
  660. /* private: */
  661. __u32 headers_start[0];
  662. /* public: */
  663. /* if you move pkt_type around you also must adapt those constants */
  664. #ifdef __BIG_ENDIAN_BITFIELD
  665. #define PKT_TYPE_MAX (7 << 5)
  666. #else
  667. #define PKT_TYPE_MAX 7
  668. #endif
  669. #define PKT_TYPE_OFFSET() offsetof(struct sk_buff, __pkt_type_offset)
  670. __u8 __pkt_type_offset[0];
  671. __u8 pkt_type:3;
  672. __u8 pfmemalloc:1;
  673. __u8 ignore_df:1;
  674. __u8 nf_trace:1;
  675. __u8 ip_summed:2;
  676. __u8 ooo_okay:1;
  677. __u8 l4_hash:1;
  678. __u8 sw_hash:1;
  679. __u8 wifi_acked_valid:1;
  680. __u8 wifi_acked:1;
  681. __u8 no_fcs:1;
  682. /* Indicates the inner headers are valid in the skbuff. */
  683. __u8 encapsulation:1;
  684. __u8 encap_hdr_csum:1;
  685. __u8 csum_valid:1;
  686. __u8 csum_complete_sw:1;
  687. __u8 csum_level:2;
  688. __u8 csum_not_inet:1;
  689. __u8 dst_pending_confirm:1;
  690. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  691. __u8 ndisc_nodetype:2;
  692. #endif
  693. __u8 ipvs_property:1;
  694. __u8 inner_protocol_type:1;
  695. __u8 remcsum_offload:1;
  696. #ifdef CONFIG_NET_SWITCHDEV
  697. __u8 offload_fwd_mark:1;
  698. __u8 offload_mr_fwd_mark:1;
  699. #endif
  700. #ifdef CONFIG_NET_CLS_ACT
  701. __u8 tc_skip_classify:1;
  702. __u8 tc_at_ingress:1;
  703. __u8 tc_redirected:1;
  704. __u8 tc_from_ingress:1;
  705. #endif
  706. #ifdef CONFIG_NET_SCHED
  707. __u16 tc_index; /* traffic control index */
  708. #endif
  709. union {
  710. __wsum csum;
  711. struct {
  712. __u16 csum_start;
  713. __u16 csum_offset;
  714. };
  715. };
  716. __u32 priority;
  717. int skb_iif;
  718. __u32 hash;
  719. __be16 vlan_proto;
  720. __u16 vlan_tci;
  721. #if defined(CONFIG_NET_RX_BUSY_POLL) || defined(CONFIG_XPS)
  722. union {
  723. unsigned int napi_id;
  724. unsigned int sender_cpu;
  725. };
  726. #endif
  727. #ifdef CONFIG_NETWORK_SECMARK
  728. __u32 secmark;
  729. #endif
  730. union {
  731. __u32 mark;
  732. __u32 reserved_tailroom;
  733. };
  734. union {
  735. __be16 inner_protocol;
  736. __u8 inner_ipproto;
  737. };
  738. __u16 inner_transport_header;
  739. __u16 inner_network_header;
  740. __u16 inner_mac_header;
  741. __be16 protocol;
  742. __u16 transport_header;
  743. __u16 network_header;
  744. __u16 mac_header;
  745. /* private: */
  746. __u32 headers_end[0];
  747. /* public: */
  748. /* These elements must be at the end, see alloc_skb() for details. */
  749. sk_buff_data_t tail;
  750. sk_buff_data_t end;
  751. unsigned char *head,
  752. *data;
  753. unsigned int truesize;
  754. refcount_t users;
  755. };
  756. #ifdef __KERNEL__
  757. /*
  758. * Handling routines are only of interest to the kernel
  759. */
  760. #include <linux/slab.h>
  761. #define SKB_ALLOC_FCLONE 0x01
  762. #define SKB_ALLOC_RX 0x02
  763. #define SKB_ALLOC_NAPI 0x04
  764. /* Returns true if the skb was allocated from PFMEMALLOC reserves */
  765. static inline bool skb_pfmemalloc(const struct sk_buff *skb)
  766. {
  767. return unlikely(skb->pfmemalloc);
  768. }
  769. /*
  770. * skb might have a dst pointer attached, refcounted or not.
  771. * _skb_refdst low order bit is set if refcount was _not_ taken
  772. */
  773. #define SKB_DST_NOREF 1UL
  774. #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
  775. #define SKB_NFCT_PTRMASK ~(7UL)
  776. /**
  777. * skb_dst - returns skb dst_entry
  778. * @skb: buffer
  779. *
  780. * Returns skb dst_entry, regardless of reference taken or not.
  781. */
  782. static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
  783. {
  784. /* If refdst was not refcounted, check we still are in a
  785. * rcu_read_lock section
  786. */
  787. WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
  788. !rcu_read_lock_held() &&
  789. !rcu_read_lock_bh_held());
  790. return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
  791. }
  792. /**
  793. * skb_dst_set - sets skb dst
  794. * @skb: buffer
  795. * @dst: dst entry
  796. *
  797. * Sets skb dst, assuming a reference was taken on dst and should
  798. * be released by skb_dst_drop()
  799. */
  800. static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
  801. {
  802. skb->_skb_refdst = (unsigned long)dst;
  803. }
  804. /**
  805. * skb_dst_set_noref - sets skb dst, hopefully, without taking reference
  806. * @skb: buffer
  807. * @dst: dst entry
  808. *
  809. * Sets skb dst, assuming a reference was not taken on dst.
  810. * If dst entry is cached, we do not take reference and dst_release
  811. * will be avoided by refdst_drop. If dst entry is not cached, we take
  812. * reference, so that last dst_release can destroy the dst immediately.
  813. */
  814. static inline void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst)
  815. {
  816. WARN_ON(!rcu_read_lock_held() && !rcu_read_lock_bh_held());
  817. skb->_skb_refdst = (unsigned long)dst | SKB_DST_NOREF;
  818. }
  819. /**
  820. * skb_dst_is_noref - Test if skb dst isn't refcounted
  821. * @skb: buffer
  822. */
  823. static inline bool skb_dst_is_noref(const struct sk_buff *skb)
  824. {
  825. return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
  826. }
  827. static inline struct rtable *skb_rtable(const struct sk_buff *skb)
  828. {
  829. return (struct rtable *)skb_dst(skb);
  830. }
  831. /* For mangling skb->pkt_type from user space side from applications
  832. * such as nft, tc, etc, we only allow a conservative subset of
  833. * possible pkt_types to be set.
  834. */
  835. static inline bool skb_pkt_type_ok(u32 ptype)
  836. {
  837. return ptype <= PACKET_OTHERHOST;
  838. }
  839. static inline unsigned int skb_napi_id(const struct sk_buff *skb)
  840. {
  841. #ifdef CONFIG_NET_RX_BUSY_POLL
  842. return skb->napi_id;
  843. #else
  844. return 0;
  845. #endif
  846. }
  847. /* decrement the reference count and return true if we can free the skb */
  848. static inline bool skb_unref(struct sk_buff *skb)
  849. {
  850. if (unlikely(!skb))
  851. return false;
  852. if (likely(refcount_read(&skb->users) == 1))
  853. smp_rmb();
  854. else if (likely(!refcount_dec_and_test(&skb->users)))
  855. return false;
  856. return true;
  857. }
  858. void skb_release_head_state(struct sk_buff *skb);
  859. void kfree_skb(struct sk_buff *skb);
  860. void kfree_skb_list(struct sk_buff *segs);
  861. void skb_tx_error(struct sk_buff *skb);
  862. void consume_skb(struct sk_buff *skb);
  863. void __consume_stateless_skb(struct sk_buff *skb);
  864. void __kfree_skb(struct sk_buff *skb);
  865. extern struct kmem_cache *skbuff_head_cache;
  866. void kfree_skb_partial(struct sk_buff *skb, bool head_stolen);
  867. bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
  868. bool *fragstolen, int *delta_truesize);
  869. struct sk_buff *__alloc_skb(unsigned int size, gfp_t priority, int flags,
  870. int node);
  871. struct sk_buff *__build_skb(void *data, unsigned int frag_size);
  872. struct sk_buff *build_skb(void *data, unsigned int frag_size);
  873. static inline struct sk_buff *alloc_skb(unsigned int size,
  874. gfp_t priority)
  875. {
  876. return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
  877. }
  878. struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
  879. unsigned long data_len,
  880. int max_page_order,
  881. int *errcode,
  882. gfp_t gfp_mask);
  883. /* Layout of fast clones : [skb1][skb2][fclone_ref] */
  884. struct sk_buff_fclones {
  885. struct sk_buff skb1;
  886. struct sk_buff skb2;
  887. refcount_t fclone_ref;
  888. };
  889. /**
  890. * skb_fclone_busy - check if fclone is busy
  891. * @sk: socket
  892. * @skb: buffer
  893. *
  894. * Returns true if skb is a fast clone, and its clone is not freed.
  895. * Some drivers call skb_orphan() in their ndo_start_xmit(),
  896. * so we also check that this didnt happen.
  897. */
  898. static inline bool skb_fclone_busy(const struct sock *sk,
  899. const struct sk_buff *skb)
  900. {
  901. const struct sk_buff_fclones *fclones;
  902. fclones = container_of(skb, struct sk_buff_fclones, skb1);
  903. return skb->fclone == SKB_FCLONE_ORIG &&
  904. refcount_read(&fclones->fclone_ref) > 1 &&
  905. fclones->skb2.sk == sk;
  906. }
  907. static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
  908. gfp_t priority)
  909. {
  910. return __alloc_skb(size, priority, SKB_ALLOC_FCLONE, NUMA_NO_NODE);
  911. }
  912. struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
  913. int skb_copy_ubufs(struct sk_buff *skb, gfp_t gfp_mask);
  914. struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t priority);
  915. struct sk_buff *skb_copy(const struct sk_buff *skb, gfp_t priority);
  916. struct sk_buff *__pskb_copy_fclone(struct sk_buff *skb, int headroom,
  917. gfp_t gfp_mask, bool fclone);
  918. static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom,
  919. gfp_t gfp_mask)
  920. {
  921. return __pskb_copy_fclone(skb, headroom, gfp_mask, false);
  922. }
  923. int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask);
  924. struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
  925. unsigned int headroom);
  926. struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom,
  927. int newtailroom, gfp_t priority);
  928. int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg,
  929. int offset, int len);
  930. int __must_check skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg,
  931. int offset, int len);
  932. int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer);
  933. int __skb_pad(struct sk_buff *skb, int pad, bool free_on_error);
  934. /**
  935. * skb_pad - zero pad the tail of an skb
  936. * @skb: buffer to pad
  937. * @pad: space to pad
  938. *
  939. * Ensure that a buffer is followed by a padding area that is zero
  940. * filled. Used by network drivers which may DMA or transfer data
  941. * beyond the buffer end onto the wire.
  942. *
  943. * May return error in out of memory cases. The skb is freed on error.
  944. */
  945. static inline int skb_pad(struct sk_buff *skb, int pad)
  946. {
  947. return __skb_pad(skb, pad, true);
  948. }
  949. #define dev_kfree_skb(a) consume_skb(a)
  950. int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  951. int getfrag(void *from, char *to, int offset,
  952. int len, int odd, struct sk_buff *skb),
  953. void *from, int length);
  954. int skb_append_pagefrags(struct sk_buff *skb, struct page *page,
  955. int offset, size_t size);
  956. struct skb_seq_state {
  957. __u32 lower_offset;
  958. __u32 upper_offset;
  959. __u32 frag_idx;
  960. __u32 stepped_offset;
  961. struct sk_buff *root_skb;
  962. struct sk_buff *cur_skb;
  963. __u8 *frag_data;
  964. };
  965. void skb_prepare_seq_read(struct sk_buff *skb, unsigned int from,
  966. unsigned int to, struct skb_seq_state *st);
  967. unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  968. struct skb_seq_state *st);
  969. void skb_abort_seq_read(struct skb_seq_state *st);
  970. unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  971. unsigned int to, struct ts_config *config);
  972. /*
  973. * Packet hash types specify the type of hash in skb_set_hash.
  974. *
  975. * Hash types refer to the protocol layer addresses which are used to
  976. * construct a packet's hash. The hashes are used to differentiate or identify
  977. * flows of the protocol layer for the hash type. Hash types are either
  978. * layer-2 (L2), layer-3 (L3), or layer-4 (L4).
  979. *
  980. * Properties of hashes:
  981. *
  982. * 1) Two packets in different flows have different hash values
  983. * 2) Two packets in the same flow should have the same hash value
  984. *
  985. * A hash at a higher layer is considered to be more specific. A driver should
  986. * set the most specific hash possible.
  987. *
  988. * A driver cannot indicate a more specific hash than the layer at which a hash
  989. * was computed. For instance an L3 hash cannot be set as an L4 hash.
  990. *
  991. * A driver may indicate a hash level which is less specific than the
  992. * actual layer the hash was computed on. For instance, a hash computed
  993. * at L4 may be considered an L3 hash. This should only be done if the
  994. * driver can't unambiguously determine that the HW computed the hash at
  995. * the higher layer. Note that the "should" in the second property above
  996. * permits this.
  997. */
  998. enum pkt_hash_types {
  999. PKT_HASH_TYPE_NONE, /* Undefined type */
  1000. PKT_HASH_TYPE_L2, /* Input: src_MAC, dest_MAC */
  1001. PKT_HASH_TYPE_L3, /* Input: src_IP, dst_IP */
  1002. PKT_HASH_TYPE_L4, /* Input: src_IP, dst_IP, src_port, dst_port */
  1003. };
  1004. static inline void skb_clear_hash(struct sk_buff *skb)
  1005. {
  1006. skb->hash = 0;
  1007. skb->sw_hash = 0;
  1008. skb->l4_hash = 0;
  1009. }
  1010. static inline void skb_clear_hash_if_not_l4(struct sk_buff *skb)
  1011. {
  1012. if (!skb->l4_hash)
  1013. skb_clear_hash(skb);
  1014. }
  1015. static inline void
  1016. __skb_set_hash(struct sk_buff *skb, __u32 hash, bool is_sw, bool is_l4)
  1017. {
  1018. skb->l4_hash = is_l4;
  1019. skb->sw_hash = is_sw;
  1020. skb->hash = hash;
  1021. }
  1022. static inline void
  1023. skb_set_hash(struct sk_buff *skb, __u32 hash, enum pkt_hash_types type)
  1024. {
  1025. /* Used by drivers to set hash from HW */
  1026. __skb_set_hash(skb, hash, false, type == PKT_HASH_TYPE_L4);
  1027. }
  1028. static inline void
  1029. __skb_set_sw_hash(struct sk_buff *skb, __u32 hash, bool is_l4)
  1030. {
  1031. __skb_set_hash(skb, hash, true, is_l4);
  1032. }
  1033. void __skb_get_hash(struct sk_buff *skb);
  1034. u32 __skb_get_hash_symmetric(const struct sk_buff *skb);
  1035. u32 skb_get_poff(const struct sk_buff *skb);
  1036. u32 __skb_get_poff(const struct sk_buff *skb, void *data,
  1037. const struct flow_keys *keys, int hlen);
  1038. __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
  1039. void *data, int hlen_proto);
  1040. static inline __be32 skb_flow_get_ports(const struct sk_buff *skb,
  1041. int thoff, u8 ip_proto)
  1042. {
  1043. return __skb_flow_get_ports(skb, thoff, ip_proto, NULL, 0);
  1044. }
  1045. void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
  1046. const struct flow_dissector_key *key,
  1047. unsigned int key_count);
  1048. bool __skb_flow_dissect(const struct sk_buff *skb,
  1049. struct flow_dissector *flow_dissector,
  1050. void *target_container,
  1051. void *data, __be16 proto, int nhoff, int hlen,
  1052. unsigned int flags);
  1053. static inline bool skb_flow_dissect(const struct sk_buff *skb,
  1054. struct flow_dissector *flow_dissector,
  1055. void *target_container, unsigned int flags)
  1056. {
  1057. return __skb_flow_dissect(skb, flow_dissector, target_container,
  1058. NULL, 0, 0, 0, flags);
  1059. }
  1060. static inline bool skb_flow_dissect_flow_keys(const struct sk_buff *skb,
  1061. struct flow_keys *flow,
  1062. unsigned int flags)
  1063. {
  1064. memset(flow, 0, sizeof(*flow));
  1065. return __skb_flow_dissect(skb, &flow_keys_dissector, flow,
  1066. NULL, 0, 0, 0, flags);
  1067. }
  1068. static inline bool skb_flow_dissect_flow_keys_buf(struct flow_keys *flow,
  1069. void *data, __be16 proto,
  1070. int nhoff, int hlen,
  1071. unsigned int flags)
  1072. {
  1073. memset(flow, 0, sizeof(*flow));
  1074. return __skb_flow_dissect(NULL, &flow_keys_buf_dissector, flow,
  1075. data, proto, nhoff, hlen, flags);
  1076. }
  1077. static inline __u32 skb_get_hash(struct sk_buff *skb)
  1078. {
  1079. if (!skb->l4_hash && !skb->sw_hash)
  1080. __skb_get_hash(skb);
  1081. return skb->hash;
  1082. }
  1083. static inline __u32 skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
  1084. {
  1085. if (!skb->l4_hash && !skb->sw_hash) {
  1086. struct flow_keys keys;
  1087. __u32 hash = __get_hash_from_flowi6(fl6, &keys);
  1088. __skb_set_sw_hash(skb, hash, flow_keys_have_l4(&keys));
  1089. }
  1090. return skb->hash;
  1091. }
  1092. __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb);
  1093. static inline __u32 skb_get_hash_raw(const struct sk_buff *skb)
  1094. {
  1095. return skb->hash;
  1096. }
  1097. static inline void skb_copy_hash(struct sk_buff *to, const struct sk_buff *from)
  1098. {
  1099. to->hash = from->hash;
  1100. to->sw_hash = from->sw_hash;
  1101. to->l4_hash = from->l4_hash;
  1102. };
  1103. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1104. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  1105. {
  1106. return skb->head + skb->end;
  1107. }
  1108. static inline unsigned int skb_end_offset(const struct sk_buff *skb)
  1109. {
  1110. return skb->end;
  1111. }
  1112. #else
  1113. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  1114. {
  1115. return skb->end;
  1116. }
  1117. static inline unsigned int skb_end_offset(const struct sk_buff *skb)
  1118. {
  1119. return skb->end - skb->head;
  1120. }
  1121. #endif
  1122. /* Internal */
  1123. #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
  1124. static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
  1125. {
  1126. return &skb_shinfo(skb)->hwtstamps;
  1127. }
  1128. static inline struct ubuf_info *skb_zcopy(struct sk_buff *skb)
  1129. {
  1130. bool is_zcopy = skb && skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY;
  1131. return is_zcopy ? skb_uarg(skb) : NULL;
  1132. }
  1133. static inline void skb_zcopy_set(struct sk_buff *skb, struct ubuf_info *uarg)
  1134. {
  1135. if (skb && uarg && !skb_zcopy(skb)) {
  1136. sock_zerocopy_get(uarg);
  1137. skb_shinfo(skb)->destructor_arg = uarg;
  1138. skb_shinfo(skb)->tx_flags |= SKBTX_ZEROCOPY_FRAG;
  1139. }
  1140. }
  1141. /* Release a reference on a zerocopy structure */
  1142. static inline void skb_zcopy_clear(struct sk_buff *skb, bool zerocopy)
  1143. {
  1144. struct ubuf_info *uarg = skb_zcopy(skb);
  1145. if (uarg) {
  1146. if (uarg->callback == sock_zerocopy_callback) {
  1147. uarg->zerocopy = uarg->zerocopy && zerocopy;
  1148. sock_zerocopy_put(uarg);
  1149. } else {
  1150. uarg->callback(uarg, zerocopy);
  1151. }
  1152. skb_shinfo(skb)->tx_flags &= ~SKBTX_ZEROCOPY_FRAG;
  1153. }
  1154. }
  1155. /* Abort a zerocopy operation and revert zckey on error in send syscall */
  1156. static inline void skb_zcopy_abort(struct sk_buff *skb)
  1157. {
  1158. struct ubuf_info *uarg = skb_zcopy(skb);
  1159. if (uarg) {
  1160. sock_zerocopy_put_abort(uarg);
  1161. skb_shinfo(skb)->tx_flags &= ~SKBTX_ZEROCOPY_FRAG;
  1162. }
  1163. }
  1164. /**
  1165. * skb_queue_empty - check if a queue is empty
  1166. * @list: queue head
  1167. *
  1168. * Returns true if the queue is empty, false otherwise.
  1169. */
  1170. static inline int skb_queue_empty(const struct sk_buff_head *list)
  1171. {
  1172. return list->next == (const struct sk_buff *) list;
  1173. }
  1174. /**
  1175. * skb_queue_is_last - check if skb is the last entry in the queue
  1176. * @list: queue head
  1177. * @skb: buffer
  1178. *
  1179. * Returns true if @skb is the last buffer on the list.
  1180. */
  1181. static inline bool skb_queue_is_last(const struct sk_buff_head *list,
  1182. const struct sk_buff *skb)
  1183. {
  1184. return skb->next == (const struct sk_buff *) list;
  1185. }
  1186. /**
  1187. * skb_queue_is_first - check if skb is the first entry in the queue
  1188. * @list: queue head
  1189. * @skb: buffer
  1190. *
  1191. * Returns true if @skb is the first buffer on the list.
  1192. */
  1193. static inline bool skb_queue_is_first(const struct sk_buff_head *list,
  1194. const struct sk_buff *skb)
  1195. {
  1196. return skb->prev == (const struct sk_buff *) list;
  1197. }
  1198. /**
  1199. * skb_queue_next - return the next packet in the queue
  1200. * @list: queue head
  1201. * @skb: current buffer
  1202. *
  1203. * Return the next packet in @list after @skb. It is only valid to
  1204. * call this if skb_queue_is_last() evaluates to false.
  1205. */
  1206. static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
  1207. const struct sk_buff *skb)
  1208. {
  1209. /* This BUG_ON may seem severe, but if we just return then we
  1210. * are going to dereference garbage.
  1211. */
  1212. BUG_ON(skb_queue_is_last(list, skb));
  1213. return skb->next;
  1214. }
  1215. /**
  1216. * skb_queue_prev - return the prev packet in the queue
  1217. * @list: queue head
  1218. * @skb: current buffer
  1219. *
  1220. * Return the prev packet in @list before @skb. It is only valid to
  1221. * call this if skb_queue_is_first() evaluates to false.
  1222. */
  1223. static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
  1224. const struct sk_buff *skb)
  1225. {
  1226. /* This BUG_ON may seem severe, but if we just return then we
  1227. * are going to dereference garbage.
  1228. */
  1229. BUG_ON(skb_queue_is_first(list, skb));
  1230. return skb->prev;
  1231. }
  1232. /**
  1233. * skb_get - reference buffer
  1234. * @skb: buffer to reference
  1235. *
  1236. * Makes another reference to a socket buffer and returns a pointer
  1237. * to the buffer.
  1238. */
  1239. static inline struct sk_buff *skb_get(struct sk_buff *skb)
  1240. {
  1241. refcount_inc(&skb->users);
  1242. return skb;
  1243. }
  1244. /*
  1245. * If users == 1, we are the only owner and are can avoid redundant
  1246. * atomic change.
  1247. */
  1248. /**
  1249. * skb_cloned - is the buffer a clone
  1250. * @skb: buffer to check
  1251. *
  1252. * Returns true if the buffer was generated with skb_clone() and is
  1253. * one of multiple shared copies of the buffer. Cloned buffers are
  1254. * shared data so must not be written to under normal circumstances.
  1255. */
  1256. static inline int skb_cloned(const struct sk_buff *skb)
  1257. {
  1258. return skb->cloned &&
  1259. (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
  1260. }
  1261. static inline int skb_unclone(struct sk_buff *skb, gfp_t pri)
  1262. {
  1263. might_sleep_if(gfpflags_allow_blocking(pri));
  1264. if (skb_cloned(skb))
  1265. return pskb_expand_head(skb, 0, 0, pri);
  1266. return 0;
  1267. }
  1268. /**
  1269. * skb_header_cloned - is the header a clone
  1270. * @skb: buffer to check
  1271. *
  1272. * Returns true if modifying the header part of the buffer requires
  1273. * the data to be copied.
  1274. */
  1275. static inline int skb_header_cloned(const struct sk_buff *skb)
  1276. {
  1277. int dataref;
  1278. if (!skb->cloned)
  1279. return 0;
  1280. dataref = atomic_read(&skb_shinfo(skb)->dataref);
  1281. dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
  1282. return dataref != 1;
  1283. }
  1284. static inline int skb_header_unclone(struct sk_buff *skb, gfp_t pri)
  1285. {
  1286. might_sleep_if(gfpflags_allow_blocking(pri));
  1287. if (skb_header_cloned(skb))
  1288. return pskb_expand_head(skb, 0, 0, pri);
  1289. return 0;
  1290. }
  1291. /**
  1292. * __skb_header_release - release reference to header
  1293. * @skb: buffer to operate on
  1294. */
  1295. static inline void __skb_header_release(struct sk_buff *skb)
  1296. {
  1297. skb->nohdr = 1;
  1298. atomic_set(&skb_shinfo(skb)->dataref, 1 + (1 << SKB_DATAREF_SHIFT));
  1299. }
  1300. /**
  1301. * skb_shared - is the buffer shared
  1302. * @skb: buffer to check
  1303. *
  1304. * Returns true if more than one person has a reference to this
  1305. * buffer.
  1306. */
  1307. static inline int skb_shared(const struct sk_buff *skb)
  1308. {
  1309. return refcount_read(&skb->users) != 1;
  1310. }
  1311. /**
  1312. * skb_share_check - check if buffer is shared and if so clone it
  1313. * @skb: buffer to check
  1314. * @pri: priority for memory allocation
  1315. *
  1316. * If the buffer is shared the buffer is cloned and the old copy
  1317. * drops a reference. A new clone with a single reference is returned.
  1318. * If the buffer is not shared the original buffer is returned. When
  1319. * being called from interrupt status or with spinlocks held pri must
  1320. * be GFP_ATOMIC.
  1321. *
  1322. * NULL is returned on a memory allocation failure.
  1323. */
  1324. static inline struct sk_buff *skb_share_check(struct sk_buff *skb, gfp_t pri)
  1325. {
  1326. might_sleep_if(gfpflags_allow_blocking(pri));
  1327. if (skb_shared(skb)) {
  1328. struct sk_buff *nskb = skb_clone(skb, pri);
  1329. if (likely(nskb))
  1330. consume_skb(skb);
  1331. else
  1332. kfree_skb(skb);
  1333. skb = nskb;
  1334. }
  1335. return skb;
  1336. }
  1337. /*
  1338. * Copy shared buffers into a new sk_buff. We effectively do COW on
  1339. * packets to handle cases where we have a local reader and forward
  1340. * and a couple of other messy ones. The normal one is tcpdumping
  1341. * a packet thats being forwarded.
  1342. */
  1343. /**
  1344. * skb_unshare - make a copy of a shared buffer
  1345. * @skb: buffer to check
  1346. * @pri: priority for memory allocation
  1347. *
  1348. * If the socket buffer is a clone then this function creates a new
  1349. * copy of the data, drops a reference count on the old copy and returns
  1350. * the new copy with the reference count at 1. If the buffer is not a clone
  1351. * the original buffer is returned. When called with a spinlock held or
  1352. * from interrupt state @pri must be %GFP_ATOMIC
  1353. *
  1354. * %NULL is returned on a memory allocation failure.
  1355. */
  1356. static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
  1357. gfp_t pri)
  1358. {
  1359. might_sleep_if(gfpflags_allow_blocking(pri));
  1360. if (skb_cloned(skb)) {
  1361. struct sk_buff *nskb = skb_copy(skb, pri);
  1362. /* Free our shared copy */
  1363. if (likely(nskb))
  1364. consume_skb(skb);
  1365. else
  1366. kfree_skb(skb);
  1367. skb = nskb;
  1368. }
  1369. return skb;
  1370. }
  1371. /**
  1372. * skb_peek - peek at the head of an &sk_buff_head
  1373. * @list_: list to peek at
  1374. *
  1375. * Peek an &sk_buff. Unlike most other operations you _MUST_
  1376. * be careful with this one. A peek leaves the buffer on the
  1377. * list and someone else may run off with it. You must hold
  1378. * the appropriate locks or have a private queue to do this.
  1379. *
  1380. * Returns %NULL for an empty list or a pointer to the head element.
  1381. * The reference count is not incremented and the reference is therefore
  1382. * volatile. Use with caution.
  1383. */
  1384. static inline struct sk_buff *skb_peek(const struct sk_buff_head *list_)
  1385. {
  1386. struct sk_buff *skb = list_->next;
  1387. if (skb == (struct sk_buff *)list_)
  1388. skb = NULL;
  1389. return skb;
  1390. }
  1391. /**
  1392. * skb_peek_next - peek skb following the given one from a queue
  1393. * @skb: skb to start from
  1394. * @list_: list to peek at
  1395. *
  1396. * Returns %NULL when the end of the list is met or a pointer to the
  1397. * next element. The reference count is not incremented and the
  1398. * reference is therefore volatile. Use with caution.
  1399. */
  1400. static inline struct sk_buff *skb_peek_next(struct sk_buff *skb,
  1401. const struct sk_buff_head *list_)
  1402. {
  1403. struct sk_buff *next = skb->next;
  1404. if (next == (struct sk_buff *)list_)
  1405. next = NULL;
  1406. return next;
  1407. }
  1408. /**
  1409. * skb_peek_tail - peek at the tail of an &sk_buff_head
  1410. * @list_: list to peek at
  1411. *
  1412. * Peek an &sk_buff. Unlike most other operations you _MUST_
  1413. * be careful with this one. A peek leaves the buffer on the
  1414. * list and someone else may run off with it. You must hold
  1415. * the appropriate locks or have a private queue to do this.
  1416. *
  1417. * Returns %NULL for an empty list or a pointer to the tail element.
  1418. * The reference count is not incremented and the reference is therefore
  1419. * volatile. Use with caution.
  1420. */
  1421. static inline struct sk_buff *skb_peek_tail(const struct sk_buff_head *list_)
  1422. {
  1423. struct sk_buff *skb = list_->prev;
  1424. if (skb == (struct sk_buff *)list_)
  1425. skb = NULL;
  1426. return skb;
  1427. }
  1428. /**
  1429. * skb_queue_len - get queue length
  1430. * @list_: list to measure
  1431. *
  1432. * Return the length of an &sk_buff queue.
  1433. */
  1434. static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
  1435. {
  1436. return list_->qlen;
  1437. }
  1438. /**
  1439. * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
  1440. * @list: queue to initialize
  1441. *
  1442. * This initializes only the list and queue length aspects of
  1443. * an sk_buff_head object. This allows to initialize the list
  1444. * aspects of an sk_buff_head without reinitializing things like
  1445. * the spinlock. It can also be used for on-stack sk_buff_head
  1446. * objects where the spinlock is known to not be used.
  1447. */
  1448. static inline void __skb_queue_head_init(struct sk_buff_head *list)
  1449. {
  1450. list->prev = list->next = (struct sk_buff *)list;
  1451. list->qlen = 0;
  1452. }
  1453. /*
  1454. * This function creates a split out lock class for each invocation;
  1455. * this is needed for now since a whole lot of users of the skb-queue
  1456. * infrastructure in drivers have different locking usage (in hardirq)
  1457. * than the networking core (in softirq only). In the long run either the
  1458. * network layer or drivers should need annotation to consolidate the
  1459. * main types of usage into 3 classes.
  1460. */
  1461. static inline void skb_queue_head_init(struct sk_buff_head *list)
  1462. {
  1463. spin_lock_init(&list->lock);
  1464. __skb_queue_head_init(list);
  1465. }
  1466. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  1467. struct lock_class_key *class)
  1468. {
  1469. skb_queue_head_init(list);
  1470. lockdep_set_class(&list->lock, class);
  1471. }
  1472. /*
  1473. * Insert an sk_buff on a list.
  1474. *
  1475. * The "__skb_xxxx()" functions are the non-atomic ones that
  1476. * can only be called with interrupts disabled.
  1477. */
  1478. void skb_insert(struct sk_buff *old, struct sk_buff *newsk,
  1479. struct sk_buff_head *list);
  1480. static inline void __skb_insert(struct sk_buff *newsk,
  1481. struct sk_buff *prev, struct sk_buff *next,
  1482. struct sk_buff_head *list)
  1483. {
  1484. newsk->next = next;
  1485. newsk->prev = prev;
  1486. next->prev = prev->next = newsk;
  1487. list->qlen++;
  1488. }
  1489. static inline void __skb_queue_splice(const struct sk_buff_head *list,
  1490. struct sk_buff *prev,
  1491. struct sk_buff *next)
  1492. {
  1493. struct sk_buff *first = list->next;
  1494. struct sk_buff *last = list->prev;
  1495. first->prev = prev;
  1496. prev->next = first;
  1497. last->next = next;
  1498. next->prev = last;
  1499. }
  1500. /**
  1501. * skb_queue_splice - join two skb lists, this is designed for stacks
  1502. * @list: the new list to add
  1503. * @head: the place to add it in the first list
  1504. */
  1505. static inline void skb_queue_splice(const struct sk_buff_head *list,
  1506. struct sk_buff_head *head)
  1507. {
  1508. if (!skb_queue_empty(list)) {
  1509. __skb_queue_splice(list, (struct sk_buff *) head, head->next);
  1510. head->qlen += list->qlen;
  1511. }
  1512. }
  1513. /**
  1514. * skb_queue_splice_init - join two skb lists and reinitialise the emptied list
  1515. * @list: the new list to add
  1516. * @head: the place to add it in the first list
  1517. *
  1518. * The list at @list is reinitialised
  1519. */
  1520. static inline void skb_queue_splice_init(struct sk_buff_head *list,
  1521. struct sk_buff_head *head)
  1522. {
  1523. if (!skb_queue_empty(list)) {
  1524. __skb_queue_splice(list, (struct sk_buff *) head, head->next);
  1525. head->qlen += list->qlen;
  1526. __skb_queue_head_init(list);
  1527. }
  1528. }
  1529. /**
  1530. * skb_queue_splice_tail - join two skb lists, each list being a queue
  1531. * @list: the new list to add
  1532. * @head: the place to add it in the first list
  1533. */
  1534. static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
  1535. struct sk_buff_head *head)
  1536. {
  1537. if (!skb_queue_empty(list)) {
  1538. __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
  1539. head->qlen += list->qlen;
  1540. }
  1541. }
  1542. /**
  1543. * skb_queue_splice_tail_init - join two skb lists and reinitialise the emptied list
  1544. * @list: the new list to add
  1545. * @head: the place to add it in the first list
  1546. *
  1547. * Each of the lists is a queue.
  1548. * The list at @list is reinitialised
  1549. */
  1550. static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
  1551. struct sk_buff_head *head)
  1552. {
  1553. if (!skb_queue_empty(list)) {
  1554. __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
  1555. head->qlen += list->qlen;
  1556. __skb_queue_head_init(list);
  1557. }
  1558. }
  1559. /**
  1560. * __skb_queue_after - queue a buffer at the list head
  1561. * @list: list to use
  1562. * @prev: place after this buffer
  1563. * @newsk: buffer to queue
  1564. *
  1565. * Queue a buffer int the middle of a list. This function takes no locks
  1566. * and you must therefore hold required locks before calling it.
  1567. *
  1568. * A buffer cannot be placed on two lists at the same time.
  1569. */
  1570. static inline void __skb_queue_after(struct sk_buff_head *list,
  1571. struct sk_buff *prev,
  1572. struct sk_buff *newsk)
  1573. {
  1574. __skb_insert(newsk, prev, prev->next, list);
  1575. }
  1576. void skb_append(struct sk_buff *old, struct sk_buff *newsk,
  1577. struct sk_buff_head *list);
  1578. static inline void __skb_queue_before(struct sk_buff_head *list,
  1579. struct sk_buff *next,
  1580. struct sk_buff *newsk)
  1581. {
  1582. __skb_insert(newsk, next->prev, next, list);
  1583. }
  1584. /**
  1585. * __skb_queue_head - queue a buffer at the list head
  1586. * @list: list to use
  1587. * @newsk: buffer to queue
  1588. *
  1589. * Queue a buffer at the start of a list. This function takes no locks
  1590. * and you must therefore hold required locks before calling it.
  1591. *
  1592. * A buffer cannot be placed on two lists at the same time.
  1593. */
  1594. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
  1595. static inline void __skb_queue_head(struct sk_buff_head *list,
  1596. struct sk_buff *newsk)
  1597. {
  1598. __skb_queue_after(list, (struct sk_buff *)list, newsk);
  1599. }
  1600. /**
  1601. * __skb_queue_tail - queue a buffer at the list tail
  1602. * @list: list to use
  1603. * @newsk: buffer to queue
  1604. *
  1605. * Queue a buffer at the end of a list. This function takes no locks
  1606. * and you must therefore hold required locks before calling it.
  1607. *
  1608. * A buffer cannot be placed on two lists at the same time.
  1609. */
  1610. void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
  1611. static inline void __skb_queue_tail(struct sk_buff_head *list,
  1612. struct sk_buff *newsk)
  1613. {
  1614. __skb_queue_before(list, (struct sk_buff *)list, newsk);
  1615. }
  1616. /*
  1617. * remove sk_buff from list. _Must_ be called atomically, and with
  1618. * the list known..
  1619. */
  1620. void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
  1621. static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  1622. {
  1623. struct sk_buff *next, *prev;
  1624. list->qlen--;
  1625. next = skb->next;
  1626. prev = skb->prev;
  1627. skb->next = skb->prev = NULL;
  1628. next->prev = prev;
  1629. prev->next = next;
  1630. }
  1631. /**
  1632. * __skb_dequeue - remove from the head of the queue
  1633. * @list: list to dequeue from
  1634. *
  1635. * Remove the head of the list. This function does not take any locks
  1636. * so must be used with appropriate locks held only. The head item is
  1637. * returned or %NULL if the list is empty.
  1638. */
  1639. struct sk_buff *skb_dequeue(struct sk_buff_head *list);
  1640. static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
  1641. {
  1642. struct sk_buff *skb = skb_peek(list);
  1643. if (skb)
  1644. __skb_unlink(skb, list);
  1645. return skb;
  1646. }
  1647. /**
  1648. * __skb_dequeue_tail - remove from the tail of the queue
  1649. * @list: list to dequeue from
  1650. *
  1651. * Remove the tail of the list. This function does not take any locks
  1652. * so must be used with appropriate locks held only. The tail item is
  1653. * returned or %NULL if the list is empty.
  1654. */
  1655. struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
  1656. static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
  1657. {
  1658. struct sk_buff *skb = skb_peek_tail(list);
  1659. if (skb)
  1660. __skb_unlink(skb, list);
  1661. return skb;
  1662. }
  1663. static inline bool skb_is_nonlinear(const struct sk_buff *skb)
  1664. {
  1665. return skb->data_len;
  1666. }
  1667. static inline unsigned int skb_headlen(const struct sk_buff *skb)
  1668. {
  1669. return skb->len - skb->data_len;
  1670. }
  1671. static inline unsigned int __skb_pagelen(const struct sk_buff *skb)
  1672. {
  1673. unsigned int i, len = 0;
  1674. for (i = skb_shinfo(skb)->nr_frags - 1; (int)i >= 0; i--)
  1675. len += skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1676. return len;
  1677. }
  1678. static inline unsigned int skb_pagelen(const struct sk_buff *skb)
  1679. {
  1680. return skb_headlen(skb) + __skb_pagelen(skb);
  1681. }
  1682. /**
  1683. * __skb_fill_page_desc - initialise a paged fragment in an skb
  1684. * @skb: buffer containing fragment to be initialised
  1685. * @i: paged fragment index to initialise
  1686. * @page: the page to use for this fragment
  1687. * @off: the offset to the data with @page
  1688. * @size: the length of the data
  1689. *
  1690. * Initialises the @i'th fragment of @skb to point to &size bytes at
  1691. * offset @off within @page.
  1692. *
  1693. * Does not take any additional reference on the fragment.
  1694. */
  1695. static inline void __skb_fill_page_desc(struct sk_buff *skb, int i,
  1696. struct page *page, int off, int size)
  1697. {
  1698. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1699. /*
  1700. * Propagate page pfmemalloc to the skb if we can. The problem is
  1701. * that not all callers have unique ownership of the page but rely
  1702. * on page_is_pfmemalloc doing the right thing(tm).
  1703. */
  1704. frag->page.p = page;
  1705. frag->page_offset = off;
  1706. skb_frag_size_set(frag, size);
  1707. page = compound_head(page);
  1708. if (page_is_pfmemalloc(page))
  1709. skb->pfmemalloc = true;
  1710. }
  1711. /**
  1712. * skb_fill_page_desc - initialise a paged fragment in an skb
  1713. * @skb: buffer containing fragment to be initialised
  1714. * @i: paged fragment index to initialise
  1715. * @page: the page to use for this fragment
  1716. * @off: the offset to the data with @page
  1717. * @size: the length of the data
  1718. *
  1719. * As per __skb_fill_page_desc() -- initialises the @i'th fragment of
  1720. * @skb to point to @size bytes at offset @off within @page. In
  1721. * addition updates @skb such that @i is the last fragment.
  1722. *
  1723. * Does not take any additional reference on the fragment.
  1724. */
  1725. static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
  1726. struct page *page, int off, int size)
  1727. {
  1728. __skb_fill_page_desc(skb, i, page, off, size);
  1729. skb_shinfo(skb)->nr_frags = i + 1;
  1730. }
  1731. void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
  1732. int size, unsigned int truesize);
  1733. void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
  1734. unsigned int truesize);
  1735. #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
  1736. #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
  1737. #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
  1738. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1739. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  1740. {
  1741. return skb->head + skb->tail;
  1742. }
  1743. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  1744. {
  1745. skb->tail = skb->data - skb->head;
  1746. }
  1747. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  1748. {
  1749. skb_reset_tail_pointer(skb);
  1750. skb->tail += offset;
  1751. }
  1752. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  1753. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  1754. {
  1755. return skb->tail;
  1756. }
  1757. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  1758. {
  1759. skb->tail = skb->data;
  1760. }
  1761. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  1762. {
  1763. skb->tail = skb->data + offset;
  1764. }
  1765. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  1766. /*
  1767. * Add data to an sk_buff
  1768. */
  1769. void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len);
  1770. void *skb_put(struct sk_buff *skb, unsigned int len);
  1771. static inline void *__skb_put(struct sk_buff *skb, unsigned int len)
  1772. {
  1773. void *tmp = skb_tail_pointer(skb);
  1774. SKB_LINEAR_ASSERT(skb);
  1775. skb->tail += len;
  1776. skb->len += len;
  1777. return tmp;
  1778. }
  1779. static inline void *__skb_put_zero(struct sk_buff *skb, unsigned int len)
  1780. {
  1781. void *tmp = __skb_put(skb, len);
  1782. memset(tmp, 0, len);
  1783. return tmp;
  1784. }
  1785. static inline void *__skb_put_data(struct sk_buff *skb, const void *data,
  1786. unsigned int len)
  1787. {
  1788. void *tmp = __skb_put(skb, len);
  1789. memcpy(tmp, data, len);
  1790. return tmp;
  1791. }
  1792. static inline void __skb_put_u8(struct sk_buff *skb, u8 val)
  1793. {
  1794. *(u8 *)__skb_put(skb, 1) = val;
  1795. }
  1796. static inline void *skb_put_zero(struct sk_buff *skb, unsigned int len)
  1797. {
  1798. void *tmp = skb_put(skb, len);
  1799. memset(tmp, 0, len);
  1800. return tmp;
  1801. }
  1802. static inline void *skb_put_data(struct sk_buff *skb, const void *data,
  1803. unsigned int len)
  1804. {
  1805. void *tmp = skb_put(skb, len);
  1806. memcpy(tmp, data, len);
  1807. return tmp;
  1808. }
  1809. static inline void skb_put_u8(struct sk_buff *skb, u8 val)
  1810. {
  1811. *(u8 *)skb_put(skb, 1) = val;
  1812. }
  1813. void *skb_push(struct sk_buff *skb, unsigned int len);
  1814. static inline void *__skb_push(struct sk_buff *skb, unsigned int len)
  1815. {
  1816. skb->data -= len;
  1817. skb->len += len;
  1818. return skb->data;
  1819. }
  1820. void *skb_pull(struct sk_buff *skb, unsigned int len);
  1821. static inline void *__skb_pull(struct sk_buff *skb, unsigned int len)
  1822. {
  1823. skb->len -= len;
  1824. BUG_ON(skb->len < skb->data_len);
  1825. return skb->data += len;
  1826. }
  1827. static inline void *skb_pull_inline(struct sk_buff *skb, unsigned int len)
  1828. {
  1829. return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
  1830. }
  1831. void *__pskb_pull_tail(struct sk_buff *skb, int delta);
  1832. static inline void *__pskb_pull(struct sk_buff *skb, unsigned int len)
  1833. {
  1834. if (len > skb_headlen(skb) &&
  1835. !__pskb_pull_tail(skb, len - skb_headlen(skb)))
  1836. return NULL;
  1837. skb->len -= len;
  1838. return skb->data += len;
  1839. }
  1840. static inline void *pskb_pull(struct sk_buff *skb, unsigned int len)
  1841. {
  1842. return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
  1843. }
  1844. static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
  1845. {
  1846. if (likely(len <= skb_headlen(skb)))
  1847. return 1;
  1848. if (unlikely(len > skb->len))
  1849. return 0;
  1850. return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
  1851. }
  1852. void skb_condense(struct sk_buff *skb);
  1853. /**
  1854. * skb_headroom - bytes at buffer head
  1855. * @skb: buffer to check
  1856. *
  1857. * Return the number of bytes of free space at the head of an &sk_buff.
  1858. */
  1859. static inline unsigned int skb_headroom(const struct sk_buff *skb)
  1860. {
  1861. return skb->data - skb->head;
  1862. }
  1863. /**
  1864. * skb_tailroom - bytes at buffer end
  1865. * @skb: buffer to check
  1866. *
  1867. * Return the number of bytes of free space at the tail of an sk_buff
  1868. */
  1869. static inline int skb_tailroom(const struct sk_buff *skb)
  1870. {
  1871. return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
  1872. }
  1873. /**
  1874. * skb_availroom - bytes at buffer end
  1875. * @skb: buffer to check
  1876. *
  1877. * Return the number of bytes of free space at the tail of an sk_buff
  1878. * allocated by sk_stream_alloc()
  1879. */
  1880. static inline int skb_availroom(const struct sk_buff *skb)
  1881. {
  1882. if (skb_is_nonlinear(skb))
  1883. return 0;
  1884. return skb->end - skb->tail - skb->reserved_tailroom;
  1885. }
  1886. /**
  1887. * skb_reserve - adjust headroom
  1888. * @skb: buffer to alter
  1889. * @len: bytes to move
  1890. *
  1891. * Increase the headroom of an empty &sk_buff by reducing the tail
  1892. * room. This is only allowed for an empty buffer.
  1893. */
  1894. static inline void skb_reserve(struct sk_buff *skb, int len)
  1895. {
  1896. skb->data += len;
  1897. skb->tail += len;
  1898. }
  1899. /**
  1900. * skb_tailroom_reserve - adjust reserved_tailroom
  1901. * @skb: buffer to alter
  1902. * @mtu: maximum amount of headlen permitted
  1903. * @needed_tailroom: minimum amount of reserved_tailroom
  1904. *
  1905. * Set reserved_tailroom so that headlen can be as large as possible but
  1906. * not larger than mtu and tailroom cannot be smaller than
  1907. * needed_tailroom.
  1908. * The required headroom should already have been reserved before using
  1909. * this function.
  1910. */
  1911. static inline void skb_tailroom_reserve(struct sk_buff *skb, unsigned int mtu,
  1912. unsigned int needed_tailroom)
  1913. {
  1914. SKB_LINEAR_ASSERT(skb);
  1915. if (mtu < skb_tailroom(skb) - needed_tailroom)
  1916. /* use at most mtu */
  1917. skb->reserved_tailroom = skb_tailroom(skb) - mtu;
  1918. else
  1919. /* use up to all available space */
  1920. skb->reserved_tailroom = needed_tailroom;
  1921. }
  1922. #define ENCAP_TYPE_ETHER 0
  1923. #define ENCAP_TYPE_IPPROTO 1
  1924. static inline void skb_set_inner_protocol(struct sk_buff *skb,
  1925. __be16 protocol)
  1926. {
  1927. skb->inner_protocol = protocol;
  1928. skb->inner_protocol_type = ENCAP_TYPE_ETHER;
  1929. }
  1930. static inline void skb_set_inner_ipproto(struct sk_buff *skb,
  1931. __u8 ipproto)
  1932. {
  1933. skb->inner_ipproto = ipproto;
  1934. skb->inner_protocol_type = ENCAP_TYPE_IPPROTO;
  1935. }
  1936. static inline void skb_reset_inner_headers(struct sk_buff *skb)
  1937. {
  1938. skb->inner_mac_header = skb->mac_header;
  1939. skb->inner_network_header = skb->network_header;
  1940. skb->inner_transport_header = skb->transport_header;
  1941. }
  1942. static inline void skb_reset_mac_len(struct sk_buff *skb)
  1943. {
  1944. skb->mac_len = skb->network_header - skb->mac_header;
  1945. }
  1946. static inline unsigned char *skb_inner_transport_header(const struct sk_buff
  1947. *skb)
  1948. {
  1949. return skb->head + skb->inner_transport_header;
  1950. }
  1951. static inline int skb_inner_transport_offset(const struct sk_buff *skb)
  1952. {
  1953. return skb_inner_transport_header(skb) - skb->data;
  1954. }
  1955. static inline void skb_reset_inner_transport_header(struct sk_buff *skb)
  1956. {
  1957. skb->inner_transport_header = skb->data - skb->head;
  1958. }
  1959. static inline void skb_set_inner_transport_header(struct sk_buff *skb,
  1960. const int offset)
  1961. {
  1962. skb_reset_inner_transport_header(skb);
  1963. skb->inner_transport_header += offset;
  1964. }
  1965. static inline unsigned char *skb_inner_network_header(const struct sk_buff *skb)
  1966. {
  1967. return skb->head + skb->inner_network_header;
  1968. }
  1969. static inline void skb_reset_inner_network_header(struct sk_buff *skb)
  1970. {
  1971. skb->inner_network_header = skb->data - skb->head;
  1972. }
  1973. static inline void skb_set_inner_network_header(struct sk_buff *skb,
  1974. const int offset)
  1975. {
  1976. skb_reset_inner_network_header(skb);
  1977. skb->inner_network_header += offset;
  1978. }
  1979. static inline unsigned char *skb_inner_mac_header(const struct sk_buff *skb)
  1980. {
  1981. return skb->head + skb->inner_mac_header;
  1982. }
  1983. static inline void skb_reset_inner_mac_header(struct sk_buff *skb)
  1984. {
  1985. skb->inner_mac_header = skb->data - skb->head;
  1986. }
  1987. static inline void skb_set_inner_mac_header(struct sk_buff *skb,
  1988. const int offset)
  1989. {
  1990. skb_reset_inner_mac_header(skb);
  1991. skb->inner_mac_header += offset;
  1992. }
  1993. static inline bool skb_transport_header_was_set(const struct sk_buff *skb)
  1994. {
  1995. return skb->transport_header != (typeof(skb->transport_header))~0U;
  1996. }
  1997. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  1998. {
  1999. return skb->head + skb->transport_header;
  2000. }
  2001. static inline void skb_reset_transport_header(struct sk_buff *skb)
  2002. {
  2003. skb->transport_header = skb->data - skb->head;
  2004. }
  2005. static inline void skb_set_transport_header(struct sk_buff *skb,
  2006. const int offset)
  2007. {
  2008. skb_reset_transport_header(skb);
  2009. skb->transport_header += offset;
  2010. }
  2011. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  2012. {
  2013. return skb->head + skb->network_header;
  2014. }
  2015. static inline void skb_reset_network_header(struct sk_buff *skb)
  2016. {
  2017. skb->network_header = skb->data - skb->head;
  2018. }
  2019. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  2020. {
  2021. skb_reset_network_header(skb);
  2022. skb->network_header += offset;
  2023. }
  2024. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  2025. {
  2026. return skb->head + skb->mac_header;
  2027. }
  2028. static inline int skb_mac_offset(const struct sk_buff *skb)
  2029. {
  2030. return skb_mac_header(skb) - skb->data;
  2031. }
  2032. static inline u32 skb_mac_header_len(const struct sk_buff *skb)
  2033. {
  2034. return skb->network_header - skb->mac_header;
  2035. }
  2036. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  2037. {
  2038. return skb->mac_header != (typeof(skb->mac_header))~0U;
  2039. }
  2040. static inline void skb_reset_mac_header(struct sk_buff *skb)
  2041. {
  2042. skb->mac_header = skb->data - skb->head;
  2043. }
  2044. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  2045. {
  2046. skb_reset_mac_header(skb);
  2047. skb->mac_header += offset;
  2048. }
  2049. static inline void skb_pop_mac_header(struct sk_buff *skb)
  2050. {
  2051. skb->mac_header = skb->network_header;
  2052. }
  2053. static inline void skb_probe_transport_header(struct sk_buff *skb,
  2054. const int offset_hint)
  2055. {
  2056. struct flow_keys keys;
  2057. if (skb_transport_header_was_set(skb))
  2058. return;
  2059. else if (skb_flow_dissect_flow_keys(skb, &keys, 0))
  2060. skb_set_transport_header(skb, keys.control.thoff);
  2061. else
  2062. skb_set_transport_header(skb, offset_hint);
  2063. }
  2064. static inline void skb_mac_header_rebuild(struct sk_buff *skb)
  2065. {
  2066. if (skb_mac_header_was_set(skb)) {
  2067. const unsigned char *old_mac = skb_mac_header(skb);
  2068. skb_set_mac_header(skb, -skb->mac_len);
  2069. memmove(skb_mac_header(skb), old_mac, skb->mac_len);
  2070. }
  2071. }
  2072. static inline int skb_checksum_start_offset(const struct sk_buff *skb)
  2073. {
  2074. return skb->csum_start - skb_headroom(skb);
  2075. }
  2076. static inline unsigned char *skb_checksum_start(const struct sk_buff *skb)
  2077. {
  2078. return skb->head + skb->csum_start;
  2079. }
  2080. static inline int skb_transport_offset(const struct sk_buff *skb)
  2081. {
  2082. return skb_transport_header(skb) - skb->data;
  2083. }
  2084. static inline u32 skb_network_header_len(const struct sk_buff *skb)
  2085. {
  2086. return skb->transport_header - skb->network_header;
  2087. }
  2088. static inline u32 skb_inner_network_header_len(const struct sk_buff *skb)
  2089. {
  2090. return skb->inner_transport_header - skb->inner_network_header;
  2091. }
  2092. static inline int skb_network_offset(const struct sk_buff *skb)
  2093. {
  2094. return skb_network_header(skb) - skb->data;
  2095. }
  2096. static inline int skb_inner_network_offset(const struct sk_buff *skb)
  2097. {
  2098. return skb_inner_network_header(skb) - skb->data;
  2099. }
  2100. static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
  2101. {
  2102. return pskb_may_pull(skb, skb_network_offset(skb) + len);
  2103. }
  2104. /*
  2105. * CPUs often take a performance hit when accessing unaligned memory
  2106. * locations. The actual performance hit varies, it can be small if the
  2107. * hardware handles it or large if we have to take an exception and fix it
  2108. * in software.
  2109. *
  2110. * Since an ethernet header is 14 bytes network drivers often end up with
  2111. * the IP header at an unaligned offset. The IP header can be aligned by
  2112. * shifting the start of the packet by 2 bytes. Drivers should do this
  2113. * with:
  2114. *
  2115. * skb_reserve(skb, NET_IP_ALIGN);
  2116. *
  2117. * The downside to this alignment of the IP header is that the DMA is now
  2118. * unaligned. On some architectures the cost of an unaligned DMA is high
  2119. * and this cost outweighs the gains made by aligning the IP header.
  2120. *
  2121. * Since this trade off varies between architectures, we allow NET_IP_ALIGN
  2122. * to be overridden.
  2123. */
  2124. #ifndef NET_IP_ALIGN
  2125. #define NET_IP_ALIGN 2
  2126. #endif
  2127. /*
  2128. * The networking layer reserves some headroom in skb data (via
  2129. * dev_alloc_skb). This is used to avoid having to reallocate skb data when
  2130. * the header has to grow. In the default case, if the header has to grow
  2131. * 32 bytes or less we avoid the reallocation.
  2132. *
  2133. * Unfortunately this headroom changes the DMA alignment of the resulting
  2134. * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
  2135. * on some architectures. An architecture can override this value,
  2136. * perhaps setting it to a cacheline in size (since that will maintain
  2137. * cacheline alignment of the DMA). It must be a power of 2.
  2138. *
  2139. * Various parts of the networking layer expect at least 32 bytes of
  2140. * headroom, you should not reduce this.
  2141. *
  2142. * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
  2143. * to reduce average number of cache lines per packet.
  2144. * get_rps_cpus() for example only access one 64 bytes aligned block :
  2145. * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
  2146. */
  2147. #ifndef NET_SKB_PAD
  2148. #define NET_SKB_PAD max(32, L1_CACHE_BYTES)
  2149. #endif
  2150. int ___pskb_trim(struct sk_buff *skb, unsigned int len);
  2151. static inline void __skb_set_length(struct sk_buff *skb, unsigned int len)
  2152. {
  2153. if (unlikely(skb_is_nonlinear(skb))) {
  2154. WARN_ON(1);
  2155. return;
  2156. }
  2157. skb->len = len;
  2158. skb_set_tail_pointer(skb, len);
  2159. }
  2160. static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
  2161. {
  2162. __skb_set_length(skb, len);
  2163. }
  2164. void skb_trim(struct sk_buff *skb, unsigned int len);
  2165. static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
  2166. {
  2167. if (skb->data_len)
  2168. return ___pskb_trim(skb, len);
  2169. __skb_trim(skb, len);
  2170. return 0;
  2171. }
  2172. static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
  2173. {
  2174. return (len < skb->len) ? __pskb_trim(skb, len) : 0;
  2175. }
  2176. /**
  2177. * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
  2178. * @skb: buffer to alter
  2179. * @len: new length
  2180. *
  2181. * This is identical to pskb_trim except that the caller knows that
  2182. * the skb is not cloned so we should never get an error due to out-
  2183. * of-memory.
  2184. */
  2185. static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
  2186. {
  2187. int err = pskb_trim(skb, len);
  2188. BUG_ON(err);
  2189. }
  2190. static inline int __skb_grow(struct sk_buff *skb, unsigned int len)
  2191. {
  2192. unsigned int diff = len - skb->len;
  2193. if (skb_tailroom(skb) < diff) {
  2194. int ret = pskb_expand_head(skb, 0, diff - skb_tailroom(skb),
  2195. GFP_ATOMIC);
  2196. if (ret)
  2197. return ret;
  2198. }
  2199. __skb_set_length(skb, len);
  2200. return 0;
  2201. }
  2202. /**
  2203. * skb_orphan - orphan a buffer
  2204. * @skb: buffer to orphan
  2205. *
  2206. * If a buffer currently has an owner then we call the owner's
  2207. * destructor function and make the @skb unowned. The buffer continues
  2208. * to exist but is no longer charged to its former owner.
  2209. */
  2210. static inline void skb_orphan(struct sk_buff *skb)
  2211. {
  2212. if (skb->destructor) {
  2213. skb->destructor(skb);
  2214. skb->destructor = NULL;
  2215. skb->sk = NULL;
  2216. } else {
  2217. BUG_ON(skb->sk);
  2218. }
  2219. }
  2220. /**
  2221. * skb_orphan_frags - orphan the frags contained in a buffer
  2222. * @skb: buffer to orphan frags from
  2223. * @gfp_mask: allocation mask for replacement pages
  2224. *
  2225. * For each frag in the SKB which needs a destructor (i.e. has an
  2226. * owner) create a copy of that frag and release the original
  2227. * page by calling the destructor.
  2228. */
  2229. static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)
  2230. {
  2231. if (likely(!skb_zcopy(skb)))
  2232. return 0;
  2233. if (skb_uarg(skb)->callback == sock_zerocopy_callback)
  2234. return 0;
  2235. return skb_copy_ubufs(skb, gfp_mask);
  2236. }
  2237. /* Frags must be orphaned, even if refcounted, if skb might loop to rx path */
  2238. static inline int skb_orphan_frags_rx(struct sk_buff *skb, gfp_t gfp_mask)
  2239. {
  2240. if (likely(!skb_zcopy(skb)))
  2241. return 0;
  2242. return skb_copy_ubufs(skb, gfp_mask);
  2243. }
  2244. /**
  2245. * __skb_queue_purge - empty a list
  2246. * @list: list to empty
  2247. *
  2248. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  2249. * the list and one reference dropped. This function does not take the
  2250. * list lock and the caller must hold the relevant locks to use it.
  2251. */
  2252. void skb_queue_purge(struct sk_buff_head *list);
  2253. static inline void __skb_queue_purge(struct sk_buff_head *list)
  2254. {
  2255. struct sk_buff *skb;
  2256. while ((skb = __skb_dequeue(list)) != NULL)
  2257. kfree_skb(skb);
  2258. }
  2259. void skb_rbtree_purge(struct rb_root *root);
  2260. void *netdev_alloc_frag(unsigned int fragsz);
  2261. struct sk_buff *__netdev_alloc_skb(struct net_device *dev, unsigned int length,
  2262. gfp_t gfp_mask);
  2263. /**
  2264. * netdev_alloc_skb - allocate an skbuff for rx on a specific device
  2265. * @dev: network device to receive on
  2266. * @length: length to allocate
  2267. *
  2268. * Allocate a new &sk_buff and assign it a usage count of one. The
  2269. * buffer has unspecified headroom built in. Users should allocate
  2270. * the headroom they think they need without accounting for the
  2271. * built in space. The built in space is used for optimisations.
  2272. *
  2273. * %NULL is returned if there is no free memory. Although this function
  2274. * allocates memory it can be called from an interrupt.
  2275. */
  2276. static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
  2277. unsigned int length)
  2278. {
  2279. return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
  2280. }
  2281. /* legacy helper around __netdev_alloc_skb() */
  2282. static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
  2283. gfp_t gfp_mask)
  2284. {
  2285. return __netdev_alloc_skb(NULL, length, gfp_mask);
  2286. }
  2287. /* legacy helper around netdev_alloc_skb() */
  2288. static inline struct sk_buff *dev_alloc_skb(unsigned int length)
  2289. {
  2290. return netdev_alloc_skb(NULL, length);
  2291. }
  2292. static inline struct sk_buff *__netdev_alloc_skb_ip_align(struct net_device *dev,
  2293. unsigned int length, gfp_t gfp)
  2294. {
  2295. struct sk_buff *skb = __netdev_alloc_skb(dev, length + NET_IP_ALIGN, gfp);
  2296. if (NET_IP_ALIGN && skb)
  2297. skb_reserve(skb, NET_IP_ALIGN);
  2298. return skb;
  2299. }
  2300. static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
  2301. unsigned int length)
  2302. {
  2303. return __netdev_alloc_skb_ip_align(dev, length, GFP_ATOMIC);
  2304. }
  2305. static inline void skb_free_frag(void *addr)
  2306. {
  2307. page_frag_free(addr);
  2308. }
  2309. void *napi_alloc_frag(unsigned int fragsz);
  2310. struct sk_buff *__napi_alloc_skb(struct napi_struct *napi,
  2311. unsigned int length, gfp_t gfp_mask);
  2312. static inline struct sk_buff *napi_alloc_skb(struct napi_struct *napi,
  2313. unsigned int length)
  2314. {
  2315. return __napi_alloc_skb(napi, length, GFP_ATOMIC);
  2316. }
  2317. void napi_consume_skb(struct sk_buff *skb, int budget);
  2318. void __kfree_skb_flush(void);
  2319. void __kfree_skb_defer(struct sk_buff *skb);
  2320. /**
  2321. * __dev_alloc_pages - allocate page for network Rx
  2322. * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
  2323. * @order: size of the allocation
  2324. *
  2325. * Allocate a new page.
  2326. *
  2327. * %NULL is returned if there is no free memory.
  2328. */
  2329. static inline struct page *__dev_alloc_pages(gfp_t gfp_mask,
  2330. unsigned int order)
  2331. {
  2332. /* This piece of code contains several assumptions.
  2333. * 1. This is for device Rx, therefor a cold page is preferred.
  2334. * 2. The expectation is the user wants a compound page.
  2335. * 3. If requesting a order 0 page it will not be compound
  2336. * due to the check to see if order has a value in prep_new_page
  2337. * 4. __GFP_MEMALLOC is ignored if __GFP_NOMEMALLOC is set due to
  2338. * code in gfp_to_alloc_flags that should be enforcing this.
  2339. */
  2340. gfp_mask |= __GFP_COLD | __GFP_COMP | __GFP_MEMALLOC;
  2341. return alloc_pages_node(NUMA_NO_NODE, gfp_mask, order);
  2342. }
  2343. static inline struct page *dev_alloc_pages(unsigned int order)
  2344. {
  2345. return __dev_alloc_pages(GFP_ATOMIC | __GFP_NOWARN, order);
  2346. }
  2347. /**
  2348. * __dev_alloc_page - allocate a page for network Rx
  2349. * @gfp_mask: allocation priority. Set __GFP_NOMEMALLOC if not for network Rx
  2350. *
  2351. * Allocate a new page.
  2352. *
  2353. * %NULL is returned if there is no free memory.
  2354. */
  2355. static inline struct page *__dev_alloc_page(gfp_t gfp_mask)
  2356. {
  2357. return __dev_alloc_pages(gfp_mask, 0);
  2358. }
  2359. static inline struct page *dev_alloc_page(void)
  2360. {
  2361. return dev_alloc_pages(0);
  2362. }
  2363. /**
  2364. * skb_propagate_pfmemalloc - Propagate pfmemalloc if skb is allocated after RX page
  2365. * @page: The page that was allocated from skb_alloc_page
  2366. * @skb: The skb that may need pfmemalloc set
  2367. */
  2368. static inline void skb_propagate_pfmemalloc(struct page *page,
  2369. struct sk_buff *skb)
  2370. {
  2371. if (page_is_pfmemalloc(page))
  2372. skb->pfmemalloc = true;
  2373. }
  2374. /**
  2375. * skb_frag_page - retrieve the page referred to by a paged fragment
  2376. * @frag: the paged fragment
  2377. *
  2378. * Returns the &struct page associated with @frag.
  2379. */
  2380. static inline struct page *skb_frag_page(const skb_frag_t *frag)
  2381. {
  2382. return frag->page.p;
  2383. }
  2384. /**
  2385. * __skb_frag_ref - take an addition reference on a paged fragment.
  2386. * @frag: the paged fragment
  2387. *
  2388. * Takes an additional reference on the paged fragment @frag.
  2389. */
  2390. static inline void __skb_frag_ref(skb_frag_t *frag)
  2391. {
  2392. get_page(skb_frag_page(frag));
  2393. }
  2394. /**
  2395. * skb_frag_ref - take an addition reference on a paged fragment of an skb.
  2396. * @skb: the buffer
  2397. * @f: the fragment offset.
  2398. *
  2399. * Takes an additional reference on the @f'th paged fragment of @skb.
  2400. */
  2401. static inline void skb_frag_ref(struct sk_buff *skb, int f)
  2402. {
  2403. __skb_frag_ref(&skb_shinfo(skb)->frags[f]);
  2404. }
  2405. /**
  2406. * __skb_frag_unref - release a reference on a paged fragment.
  2407. * @frag: the paged fragment
  2408. *
  2409. * Releases a reference on the paged fragment @frag.
  2410. */
  2411. static inline void __skb_frag_unref(skb_frag_t *frag)
  2412. {
  2413. put_page(skb_frag_page(frag));
  2414. }
  2415. /**
  2416. * skb_frag_unref - release a reference on a paged fragment of an skb.
  2417. * @skb: the buffer
  2418. * @f: the fragment offset
  2419. *
  2420. * Releases a reference on the @f'th paged fragment of @skb.
  2421. */
  2422. static inline void skb_frag_unref(struct sk_buff *skb, int f)
  2423. {
  2424. __skb_frag_unref(&skb_shinfo(skb)->frags[f]);
  2425. }
  2426. /**
  2427. * skb_frag_address - gets the address of the data contained in a paged fragment
  2428. * @frag: the paged fragment buffer
  2429. *
  2430. * Returns the address of the data within @frag. The page must already
  2431. * be mapped.
  2432. */
  2433. static inline void *skb_frag_address(const skb_frag_t *frag)
  2434. {
  2435. return page_address(skb_frag_page(frag)) + frag->page_offset;
  2436. }
  2437. /**
  2438. * skb_frag_address_safe - gets the address of the data contained in a paged fragment
  2439. * @frag: the paged fragment buffer
  2440. *
  2441. * Returns the address of the data within @frag. Checks that the page
  2442. * is mapped and returns %NULL otherwise.
  2443. */
  2444. static inline void *skb_frag_address_safe(const skb_frag_t *frag)
  2445. {
  2446. void *ptr = page_address(skb_frag_page(frag));
  2447. if (unlikely(!ptr))
  2448. return NULL;
  2449. return ptr + frag->page_offset;
  2450. }
  2451. /**
  2452. * __skb_frag_set_page - sets the page contained in a paged fragment
  2453. * @frag: the paged fragment
  2454. * @page: the page to set
  2455. *
  2456. * Sets the fragment @frag to contain @page.
  2457. */
  2458. static inline void __skb_frag_set_page(skb_frag_t *frag, struct page *page)
  2459. {
  2460. frag->page.p = page;
  2461. }
  2462. /**
  2463. * skb_frag_set_page - sets the page contained in a paged fragment of an skb
  2464. * @skb: the buffer
  2465. * @f: the fragment offset
  2466. * @page: the page to set
  2467. *
  2468. * Sets the @f'th fragment of @skb to contain @page.
  2469. */
  2470. static inline void skb_frag_set_page(struct sk_buff *skb, int f,
  2471. struct page *page)
  2472. {
  2473. __skb_frag_set_page(&skb_shinfo(skb)->frags[f], page);
  2474. }
  2475. bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio);
  2476. /**
  2477. * skb_frag_dma_map - maps a paged fragment via the DMA API
  2478. * @dev: the device to map the fragment to
  2479. * @frag: the paged fragment to map
  2480. * @offset: the offset within the fragment (starting at the
  2481. * fragment's own offset)
  2482. * @size: the number of bytes to map
  2483. * @dir: the direction of the mapping (``PCI_DMA_*``)
  2484. *
  2485. * Maps the page associated with @frag to @device.
  2486. */
  2487. static inline dma_addr_t skb_frag_dma_map(struct device *dev,
  2488. const skb_frag_t *frag,
  2489. size_t offset, size_t size,
  2490. enum dma_data_direction dir)
  2491. {
  2492. return dma_map_page(dev, skb_frag_page(frag),
  2493. frag->page_offset + offset, size, dir);
  2494. }
  2495. static inline struct sk_buff *pskb_copy(struct sk_buff *skb,
  2496. gfp_t gfp_mask)
  2497. {
  2498. return __pskb_copy(skb, skb_headroom(skb), gfp_mask);
  2499. }
  2500. static inline struct sk_buff *pskb_copy_for_clone(struct sk_buff *skb,
  2501. gfp_t gfp_mask)
  2502. {
  2503. return __pskb_copy_fclone(skb, skb_headroom(skb), gfp_mask, true);
  2504. }
  2505. /**
  2506. * skb_clone_writable - is the header of a clone writable
  2507. * @skb: buffer to check
  2508. * @len: length up to which to write
  2509. *
  2510. * Returns true if modifying the header part of the cloned buffer
  2511. * does not requires the data to be copied.
  2512. */
  2513. static inline int skb_clone_writable(const struct sk_buff *skb, unsigned int len)
  2514. {
  2515. return !skb_header_cloned(skb) &&
  2516. skb_headroom(skb) + len <= skb->hdr_len;
  2517. }
  2518. static inline int skb_try_make_writable(struct sk_buff *skb,
  2519. unsigned int write_len)
  2520. {
  2521. return skb_cloned(skb) && !skb_clone_writable(skb, write_len) &&
  2522. pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2523. }
  2524. static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
  2525. int cloned)
  2526. {
  2527. int delta = 0;
  2528. if (headroom > skb_headroom(skb))
  2529. delta = headroom - skb_headroom(skb);
  2530. if (delta || cloned)
  2531. return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
  2532. GFP_ATOMIC);
  2533. return 0;
  2534. }
  2535. /**
  2536. * skb_cow - copy header of skb when it is required
  2537. * @skb: buffer to cow
  2538. * @headroom: needed headroom
  2539. *
  2540. * If the skb passed lacks sufficient headroom or its data part
  2541. * is shared, data is reallocated. If reallocation fails, an error
  2542. * is returned and original skb is not changed.
  2543. *
  2544. * The result is skb with writable area skb->head...skb->tail
  2545. * and at least @headroom of space at head.
  2546. */
  2547. static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
  2548. {
  2549. return __skb_cow(skb, headroom, skb_cloned(skb));
  2550. }
  2551. /**
  2552. * skb_cow_head - skb_cow but only making the head writable
  2553. * @skb: buffer to cow
  2554. * @headroom: needed headroom
  2555. *
  2556. * This function is identical to skb_cow except that we replace the
  2557. * skb_cloned check by skb_header_cloned. It should be used when
  2558. * you only need to push on some header and do not need to modify
  2559. * the data.
  2560. */
  2561. static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
  2562. {
  2563. return __skb_cow(skb, headroom, skb_header_cloned(skb));
  2564. }
  2565. /**
  2566. * skb_padto - pad an skbuff up to a minimal size
  2567. * @skb: buffer to pad
  2568. * @len: minimal length
  2569. *
  2570. * Pads up a buffer to ensure the trailing bytes exist and are
  2571. * blanked. If the buffer already contains sufficient data it
  2572. * is untouched. Otherwise it is extended. Returns zero on
  2573. * success. The skb is freed on error.
  2574. */
  2575. static inline int skb_padto(struct sk_buff *skb, unsigned int len)
  2576. {
  2577. unsigned int size = skb->len;
  2578. if (likely(size >= len))
  2579. return 0;
  2580. return skb_pad(skb, len - size);
  2581. }
  2582. /**
  2583. * skb_put_padto - increase size and pad an skbuff up to a minimal size
  2584. * @skb: buffer to pad
  2585. * @len: minimal length
  2586. * @free_on_error: free buffer on error
  2587. *
  2588. * Pads up a buffer to ensure the trailing bytes exist and are
  2589. * blanked. If the buffer already contains sufficient data it
  2590. * is untouched. Otherwise it is extended. Returns zero on
  2591. * success. The skb is freed on error if @free_on_error is true.
  2592. */
  2593. static inline int __skb_put_padto(struct sk_buff *skb, unsigned int len,
  2594. bool free_on_error)
  2595. {
  2596. unsigned int size = skb->len;
  2597. if (unlikely(size < len)) {
  2598. len -= size;
  2599. if (__skb_pad(skb, len, free_on_error))
  2600. return -ENOMEM;
  2601. __skb_put(skb, len);
  2602. }
  2603. return 0;
  2604. }
  2605. /**
  2606. * skb_put_padto - increase size and pad an skbuff up to a minimal size
  2607. * @skb: buffer to pad
  2608. * @len: minimal length
  2609. *
  2610. * Pads up a buffer to ensure the trailing bytes exist and are
  2611. * blanked. If the buffer already contains sufficient data it
  2612. * is untouched. Otherwise it is extended. Returns zero on
  2613. * success. The skb is freed on error.
  2614. */
  2615. static inline int skb_put_padto(struct sk_buff *skb, unsigned int len)
  2616. {
  2617. return __skb_put_padto(skb, len, true);
  2618. }
  2619. static inline int skb_add_data(struct sk_buff *skb,
  2620. struct iov_iter *from, int copy)
  2621. {
  2622. const int off = skb->len;
  2623. if (skb->ip_summed == CHECKSUM_NONE) {
  2624. __wsum csum = 0;
  2625. if (csum_and_copy_from_iter_full(skb_put(skb, copy), copy,
  2626. &csum, from)) {
  2627. skb->csum = csum_block_add(skb->csum, csum, off);
  2628. return 0;
  2629. }
  2630. } else if (copy_from_iter_full(skb_put(skb, copy), copy, from))
  2631. return 0;
  2632. __skb_trim(skb, off);
  2633. return -EFAULT;
  2634. }
  2635. static inline bool skb_can_coalesce(struct sk_buff *skb, int i,
  2636. const struct page *page, int off)
  2637. {
  2638. if (skb_zcopy(skb))
  2639. return false;
  2640. if (i) {
  2641. const struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  2642. return page == skb_frag_page(frag) &&
  2643. off == frag->page_offset + skb_frag_size(frag);
  2644. }
  2645. return false;
  2646. }
  2647. static inline int __skb_linearize(struct sk_buff *skb)
  2648. {
  2649. return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
  2650. }
  2651. /**
  2652. * skb_linearize - convert paged skb to linear one
  2653. * @skb: buffer to linarize
  2654. *
  2655. * If there is no free memory -ENOMEM is returned, otherwise zero
  2656. * is returned and the old skb data released.
  2657. */
  2658. static inline int skb_linearize(struct sk_buff *skb)
  2659. {
  2660. return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
  2661. }
  2662. /**
  2663. * skb_has_shared_frag - can any frag be overwritten
  2664. * @skb: buffer to test
  2665. *
  2666. * Return true if the skb has at least one frag that might be modified
  2667. * by an external entity (as in vmsplice()/sendfile())
  2668. */
  2669. static inline bool skb_has_shared_frag(const struct sk_buff *skb)
  2670. {
  2671. return skb_is_nonlinear(skb) &&
  2672. skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
  2673. }
  2674. /**
  2675. * skb_linearize_cow - make sure skb is linear and writable
  2676. * @skb: buffer to process
  2677. *
  2678. * If there is no free memory -ENOMEM is returned, otherwise zero
  2679. * is returned and the old skb data released.
  2680. */
  2681. static inline int skb_linearize_cow(struct sk_buff *skb)
  2682. {
  2683. return skb_is_nonlinear(skb) || skb_cloned(skb) ?
  2684. __skb_linearize(skb) : 0;
  2685. }
  2686. static __always_inline void
  2687. __skb_postpull_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
  2688. unsigned int off)
  2689. {
  2690. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2691. skb->csum = csum_block_sub(skb->csum,
  2692. csum_partial(start, len, 0), off);
  2693. else if (skb->ip_summed == CHECKSUM_PARTIAL &&
  2694. skb_checksum_start_offset(skb) < 0)
  2695. skb->ip_summed = CHECKSUM_NONE;
  2696. }
  2697. /**
  2698. * skb_postpull_rcsum - update checksum for received skb after pull
  2699. * @skb: buffer to update
  2700. * @start: start of data before pull
  2701. * @len: length of data pulled
  2702. *
  2703. * After doing a pull on a received packet, you need to call this to
  2704. * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
  2705. * CHECKSUM_NONE so that it can be recomputed from scratch.
  2706. */
  2707. static inline void skb_postpull_rcsum(struct sk_buff *skb,
  2708. const void *start, unsigned int len)
  2709. {
  2710. __skb_postpull_rcsum(skb, start, len, 0);
  2711. }
  2712. static __always_inline void
  2713. __skb_postpush_rcsum(struct sk_buff *skb, const void *start, unsigned int len,
  2714. unsigned int off)
  2715. {
  2716. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2717. skb->csum = csum_block_add(skb->csum,
  2718. csum_partial(start, len, 0), off);
  2719. }
  2720. /**
  2721. * skb_postpush_rcsum - update checksum for received skb after push
  2722. * @skb: buffer to update
  2723. * @start: start of data after push
  2724. * @len: length of data pushed
  2725. *
  2726. * After doing a push on a received packet, you need to call this to
  2727. * update the CHECKSUM_COMPLETE checksum.
  2728. */
  2729. static inline void skb_postpush_rcsum(struct sk_buff *skb,
  2730. const void *start, unsigned int len)
  2731. {
  2732. __skb_postpush_rcsum(skb, start, len, 0);
  2733. }
  2734. void *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
  2735. /**
  2736. * skb_push_rcsum - push skb and update receive checksum
  2737. * @skb: buffer to update
  2738. * @len: length of data pulled
  2739. *
  2740. * This function performs an skb_push on the packet and updates
  2741. * the CHECKSUM_COMPLETE checksum. It should be used on
  2742. * receive path processing instead of skb_push unless you know
  2743. * that the checksum difference is zero (e.g., a valid IP header)
  2744. * or you are setting ip_summed to CHECKSUM_NONE.
  2745. */
  2746. static inline void *skb_push_rcsum(struct sk_buff *skb, unsigned int len)
  2747. {
  2748. skb_push(skb, len);
  2749. skb_postpush_rcsum(skb, skb->data, len);
  2750. return skb->data;
  2751. }
  2752. /**
  2753. * pskb_trim_rcsum - trim received skb and update checksum
  2754. * @skb: buffer to trim
  2755. * @len: new length
  2756. *
  2757. * This is exactly the same as pskb_trim except that it ensures the
  2758. * checksum of received packets are still valid after the operation.
  2759. */
  2760. static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
  2761. {
  2762. if (likely(len >= skb->len))
  2763. return 0;
  2764. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2765. skb->ip_summed = CHECKSUM_NONE;
  2766. return __pskb_trim(skb, len);
  2767. }
  2768. static inline int __skb_trim_rcsum(struct sk_buff *skb, unsigned int len)
  2769. {
  2770. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2771. skb->ip_summed = CHECKSUM_NONE;
  2772. __skb_trim(skb, len);
  2773. return 0;
  2774. }
  2775. static inline int __skb_grow_rcsum(struct sk_buff *skb, unsigned int len)
  2776. {
  2777. if (skb->ip_summed == CHECKSUM_COMPLETE)
  2778. skb->ip_summed = CHECKSUM_NONE;
  2779. return __skb_grow(skb, len);
  2780. }
  2781. #define skb_queue_walk(queue, skb) \
  2782. for (skb = (queue)->next; \
  2783. skb != (struct sk_buff *)(queue); \
  2784. skb = skb->next)
  2785. #define skb_queue_walk_safe(queue, skb, tmp) \
  2786. for (skb = (queue)->next, tmp = skb->next; \
  2787. skb != (struct sk_buff *)(queue); \
  2788. skb = tmp, tmp = skb->next)
  2789. #define skb_queue_walk_from(queue, skb) \
  2790. for (; skb != (struct sk_buff *)(queue); \
  2791. skb = skb->next)
  2792. #define skb_queue_walk_from_safe(queue, skb, tmp) \
  2793. for (tmp = skb->next; \
  2794. skb != (struct sk_buff *)(queue); \
  2795. skb = tmp, tmp = skb->next)
  2796. #define skb_queue_reverse_walk(queue, skb) \
  2797. for (skb = (queue)->prev; \
  2798. skb != (struct sk_buff *)(queue); \
  2799. skb = skb->prev)
  2800. #define skb_queue_reverse_walk_safe(queue, skb, tmp) \
  2801. for (skb = (queue)->prev, tmp = skb->prev; \
  2802. skb != (struct sk_buff *)(queue); \
  2803. skb = tmp, tmp = skb->prev)
  2804. #define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
  2805. for (tmp = skb->prev; \
  2806. skb != (struct sk_buff *)(queue); \
  2807. skb = tmp, tmp = skb->prev)
  2808. static inline bool skb_has_frag_list(const struct sk_buff *skb)
  2809. {
  2810. return skb_shinfo(skb)->frag_list != NULL;
  2811. }
  2812. static inline void skb_frag_list_init(struct sk_buff *skb)
  2813. {
  2814. skb_shinfo(skb)->frag_list = NULL;
  2815. }
  2816. #define skb_walk_frags(skb, iter) \
  2817. for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
  2818. int __skb_wait_for_more_packets(struct sock *sk, int *err, long *timeo_p,
  2819. const struct sk_buff *skb);
  2820. struct sk_buff *__skb_try_recv_from_queue(struct sock *sk,
  2821. struct sk_buff_head *queue,
  2822. unsigned int flags,
  2823. void (*destructor)(struct sock *sk,
  2824. struct sk_buff *skb),
  2825. int *peeked, int *off, int *err,
  2826. struct sk_buff **last);
  2827. struct sk_buff *__skb_try_recv_datagram(struct sock *sk, unsigned flags,
  2828. void (*destructor)(struct sock *sk,
  2829. struct sk_buff *skb),
  2830. int *peeked, int *off, int *err,
  2831. struct sk_buff **last);
  2832. struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
  2833. void (*destructor)(struct sock *sk,
  2834. struct sk_buff *skb),
  2835. int *peeked, int *off, int *err);
  2836. struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags, int noblock,
  2837. int *err);
  2838. unsigned int datagram_poll(struct file *file, struct socket *sock,
  2839. struct poll_table_struct *wait);
  2840. int skb_copy_datagram_iter(const struct sk_buff *from, int offset,
  2841. struct iov_iter *to, int size);
  2842. static inline int skb_copy_datagram_msg(const struct sk_buff *from, int offset,
  2843. struct msghdr *msg, int size)
  2844. {
  2845. return skb_copy_datagram_iter(from, offset, &msg->msg_iter, size);
  2846. }
  2847. int skb_copy_and_csum_datagram_msg(struct sk_buff *skb, int hlen,
  2848. struct msghdr *msg);
  2849. int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
  2850. struct iov_iter *from, int len);
  2851. int zerocopy_sg_from_iter(struct sk_buff *skb, struct iov_iter *frm);
  2852. void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
  2853. void __skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb, int len);
  2854. static inline void skb_free_datagram_locked(struct sock *sk,
  2855. struct sk_buff *skb)
  2856. {
  2857. __skb_free_datagram_locked(sk, skb, 0);
  2858. }
  2859. int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags);
  2860. int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);
  2861. int skb_store_bits(struct sk_buff *skb, int offset, const void *from, int len);
  2862. __wsum skb_copy_and_csum_bits(const struct sk_buff *skb, int offset, u8 *to,
  2863. int len, __wsum csum);
  2864. int skb_splice_bits(struct sk_buff *skb, struct sock *sk, unsigned int offset,
  2865. struct pipe_inode_info *pipe, unsigned int len,
  2866. unsigned int flags);
  2867. int skb_send_sock_locked(struct sock *sk, struct sk_buff *skb, int offset,
  2868. int len);
  2869. int skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset, int len);
  2870. void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
  2871. unsigned int skb_zerocopy_headlen(const struct sk_buff *from);
  2872. int skb_zerocopy(struct sk_buff *to, struct sk_buff *from,
  2873. int len, int hlen);
  2874. void skb_split(struct sk_buff *skb, struct sk_buff *skb1, const u32 len);
  2875. int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen);
  2876. void skb_scrub_packet(struct sk_buff *skb, bool xnet);
  2877. unsigned int skb_gso_transport_seglen(const struct sk_buff *skb);
  2878. bool skb_gso_validate_mtu(const struct sk_buff *skb, unsigned int mtu);
  2879. struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features);
  2880. struct sk_buff *skb_vlan_untag(struct sk_buff *skb);
  2881. int skb_ensure_writable(struct sk_buff *skb, int write_len);
  2882. int __skb_vlan_pop(struct sk_buff *skb, u16 *vlan_tci);
  2883. int skb_vlan_pop(struct sk_buff *skb);
  2884. int skb_vlan_push(struct sk_buff *skb, __be16 vlan_proto, u16 vlan_tci);
  2885. struct sk_buff *pskb_extract(struct sk_buff *skb, int off, int to_copy,
  2886. gfp_t gfp);
  2887. static inline int memcpy_from_msg(void *data, struct msghdr *msg, int len)
  2888. {
  2889. return copy_from_iter_full(data, len, &msg->msg_iter) ? 0 : -EFAULT;
  2890. }
  2891. static inline int memcpy_to_msg(struct msghdr *msg, void *data, int len)
  2892. {
  2893. return copy_to_iter(data, len, &msg->msg_iter) == len ? 0 : -EFAULT;
  2894. }
  2895. struct skb_checksum_ops {
  2896. __wsum (*update)(const void *mem, int len, __wsum wsum);
  2897. __wsum (*combine)(__wsum csum, __wsum csum2, int offset, int len);
  2898. };
  2899. extern const struct skb_checksum_ops *crc32c_csum_stub __read_mostly;
  2900. __wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
  2901. __wsum csum, const struct skb_checksum_ops *ops);
  2902. __wsum skb_checksum(const struct sk_buff *skb, int offset, int len,
  2903. __wsum csum);
  2904. static inline void * __must_check
  2905. __skb_header_pointer(const struct sk_buff *skb, int offset,
  2906. int len, void *data, int hlen, void *buffer)
  2907. {
  2908. if (hlen - offset >= len)
  2909. return data + offset;
  2910. if (!skb ||
  2911. skb_copy_bits(skb, offset, buffer, len) < 0)
  2912. return NULL;
  2913. return buffer;
  2914. }
  2915. static inline void * __must_check
  2916. skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
  2917. {
  2918. return __skb_header_pointer(skb, offset, len, skb->data,
  2919. skb_headlen(skb), buffer);
  2920. }
  2921. /**
  2922. * skb_needs_linearize - check if we need to linearize a given skb
  2923. * depending on the given device features.
  2924. * @skb: socket buffer to check
  2925. * @features: net device features
  2926. *
  2927. * Returns true if either:
  2928. * 1. skb has frag_list and the device doesn't support FRAGLIST, or
  2929. * 2. skb is fragmented and the device does not support SG.
  2930. */
  2931. static inline bool skb_needs_linearize(struct sk_buff *skb,
  2932. netdev_features_t features)
  2933. {
  2934. return skb_is_nonlinear(skb) &&
  2935. ((skb_has_frag_list(skb) && !(features & NETIF_F_FRAGLIST)) ||
  2936. (skb_shinfo(skb)->nr_frags && !(features & NETIF_F_SG)));
  2937. }
  2938. static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
  2939. void *to,
  2940. const unsigned int len)
  2941. {
  2942. memcpy(to, skb->data, len);
  2943. }
  2944. static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
  2945. const int offset, void *to,
  2946. const unsigned int len)
  2947. {
  2948. memcpy(to, skb->data + offset, len);
  2949. }
  2950. static inline void skb_copy_to_linear_data(struct sk_buff *skb,
  2951. const void *from,
  2952. const unsigned int len)
  2953. {
  2954. memcpy(skb->data, from, len);
  2955. }
  2956. static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
  2957. const int offset,
  2958. const void *from,
  2959. const unsigned int len)
  2960. {
  2961. memcpy(skb->data + offset, from, len);
  2962. }
  2963. void skb_init(void);
  2964. static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
  2965. {
  2966. return skb->tstamp;
  2967. }
  2968. /**
  2969. * skb_get_timestamp - get timestamp from a skb
  2970. * @skb: skb to get stamp from
  2971. * @stamp: pointer to struct timeval to store stamp in
  2972. *
  2973. * Timestamps are stored in the skb as offsets to a base timestamp.
  2974. * This function converts the offset back to a struct timeval and stores
  2975. * it in stamp.
  2976. */
  2977. static inline void skb_get_timestamp(const struct sk_buff *skb,
  2978. struct timeval *stamp)
  2979. {
  2980. *stamp = ktime_to_timeval(skb->tstamp);
  2981. }
  2982. static inline void skb_get_timestampns(const struct sk_buff *skb,
  2983. struct timespec *stamp)
  2984. {
  2985. *stamp = ktime_to_timespec(skb->tstamp);
  2986. }
  2987. static inline void __net_timestamp(struct sk_buff *skb)
  2988. {
  2989. skb->tstamp = ktime_get_real();
  2990. }
  2991. static inline ktime_t net_timedelta(ktime_t t)
  2992. {
  2993. return ktime_sub(ktime_get_real(), t);
  2994. }
  2995. static inline ktime_t net_invalid_timestamp(void)
  2996. {
  2997. return 0;
  2998. }
  2999. static inline u8 skb_metadata_len(const struct sk_buff *skb)
  3000. {
  3001. return skb_shinfo(skb)->meta_len;
  3002. }
  3003. static inline void *skb_metadata_end(const struct sk_buff *skb)
  3004. {
  3005. return skb_mac_header(skb);
  3006. }
  3007. static inline bool __skb_metadata_differs(const struct sk_buff *skb_a,
  3008. const struct sk_buff *skb_b,
  3009. u8 meta_len)
  3010. {
  3011. const void *a = skb_metadata_end(skb_a);
  3012. const void *b = skb_metadata_end(skb_b);
  3013. /* Using more efficient varaiant than plain call to memcmp(). */
  3014. #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
  3015. u64 diffs = 0;
  3016. switch (meta_len) {
  3017. #define __it(x, op) (x -= sizeof(u##op))
  3018. #define __it_diff(a, b, op) (*(u##op *)__it(a, op)) ^ (*(u##op *)__it(b, op))
  3019. case 32: diffs |= __it_diff(a, b, 64);
  3020. case 24: diffs |= __it_diff(a, b, 64);
  3021. case 16: diffs |= __it_diff(a, b, 64);
  3022. case 8: diffs |= __it_diff(a, b, 64);
  3023. break;
  3024. case 28: diffs |= __it_diff(a, b, 64);
  3025. case 20: diffs |= __it_diff(a, b, 64);
  3026. case 12: diffs |= __it_diff(a, b, 64);
  3027. case 4: diffs |= __it_diff(a, b, 32);
  3028. break;
  3029. }
  3030. return diffs;
  3031. #else
  3032. return memcmp(a - meta_len, b - meta_len, meta_len);
  3033. #endif
  3034. }
  3035. static inline bool skb_metadata_differs(const struct sk_buff *skb_a,
  3036. const struct sk_buff *skb_b)
  3037. {
  3038. u8 len_a = skb_metadata_len(skb_a);
  3039. u8 len_b = skb_metadata_len(skb_b);
  3040. if (!(len_a | len_b))
  3041. return false;
  3042. return len_a != len_b ?
  3043. true : __skb_metadata_differs(skb_a, skb_b, len_a);
  3044. }
  3045. static inline void skb_metadata_set(struct sk_buff *skb, u8 meta_len)
  3046. {
  3047. skb_shinfo(skb)->meta_len = meta_len;
  3048. }
  3049. static inline void skb_metadata_clear(struct sk_buff *skb)
  3050. {
  3051. skb_metadata_set(skb, 0);
  3052. }
  3053. struct sk_buff *skb_clone_sk(struct sk_buff *skb);
  3054. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  3055. void skb_clone_tx_timestamp(struct sk_buff *skb);
  3056. bool skb_defer_rx_timestamp(struct sk_buff *skb);
  3057. #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
  3058. static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
  3059. {
  3060. }
  3061. static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
  3062. {
  3063. return false;
  3064. }
  3065. #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
  3066. /**
  3067. * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
  3068. *
  3069. * PHY drivers may accept clones of transmitted packets for
  3070. * timestamping via their phy_driver.txtstamp method. These drivers
  3071. * must call this function to return the skb back to the stack with a
  3072. * timestamp.
  3073. *
  3074. * @skb: clone of the the original outgoing packet
  3075. * @hwtstamps: hardware time stamps
  3076. *
  3077. */
  3078. void skb_complete_tx_timestamp(struct sk_buff *skb,
  3079. struct skb_shared_hwtstamps *hwtstamps);
  3080. void __skb_tstamp_tx(struct sk_buff *orig_skb,
  3081. struct skb_shared_hwtstamps *hwtstamps,
  3082. struct sock *sk, int tstype);
  3083. /**
  3084. * skb_tstamp_tx - queue clone of skb with send time stamps
  3085. * @orig_skb: the original outgoing packet
  3086. * @hwtstamps: hardware time stamps, may be NULL if not available
  3087. *
  3088. * If the skb has a socket associated, then this function clones the
  3089. * skb (thus sharing the actual data and optional structures), stores
  3090. * the optional hardware time stamping information (if non NULL) or
  3091. * generates a software time stamp (otherwise), then queues the clone
  3092. * to the error queue of the socket. Errors are silently ignored.
  3093. */
  3094. void skb_tstamp_tx(struct sk_buff *orig_skb,
  3095. struct skb_shared_hwtstamps *hwtstamps);
  3096. /**
  3097. * skb_tx_timestamp() - Driver hook for transmit timestamping
  3098. *
  3099. * Ethernet MAC Drivers should call this function in their hard_xmit()
  3100. * function immediately before giving the sk_buff to the MAC hardware.
  3101. *
  3102. * Specifically, one should make absolutely sure that this function is
  3103. * called before TX completion of this packet can trigger. Otherwise
  3104. * the packet could potentially already be freed.
  3105. *
  3106. * @skb: A socket buffer.
  3107. */
  3108. static inline void skb_tx_timestamp(struct sk_buff *skb)
  3109. {
  3110. skb_clone_tx_timestamp(skb);
  3111. if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP)
  3112. skb_tstamp_tx(skb, NULL);
  3113. }
  3114. /**
  3115. * skb_complete_wifi_ack - deliver skb with wifi status
  3116. *
  3117. * @skb: the original outgoing packet
  3118. * @acked: ack status
  3119. *
  3120. */
  3121. void skb_complete_wifi_ack(struct sk_buff *skb, bool acked);
  3122. __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
  3123. __sum16 __skb_checksum_complete(struct sk_buff *skb);
  3124. static inline int skb_csum_unnecessary(const struct sk_buff *skb)
  3125. {
  3126. return ((skb->ip_summed == CHECKSUM_UNNECESSARY) ||
  3127. skb->csum_valid ||
  3128. (skb->ip_summed == CHECKSUM_PARTIAL &&
  3129. skb_checksum_start_offset(skb) >= 0));
  3130. }
  3131. /**
  3132. * skb_checksum_complete - Calculate checksum of an entire packet
  3133. * @skb: packet to process
  3134. *
  3135. * This function calculates the checksum over the entire packet plus
  3136. * the value of skb->csum. The latter can be used to supply the
  3137. * checksum of a pseudo header as used by TCP/UDP. It returns the
  3138. * checksum.
  3139. *
  3140. * For protocols that contain complete checksums such as ICMP/TCP/UDP,
  3141. * this function can be used to verify that checksum on received
  3142. * packets. In that case the function should return zero if the
  3143. * checksum is correct. In particular, this function will return zero
  3144. * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
  3145. * hardware has already verified the correctness of the checksum.
  3146. */
  3147. static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
  3148. {
  3149. return skb_csum_unnecessary(skb) ?
  3150. 0 : __skb_checksum_complete(skb);
  3151. }
  3152. static inline void __skb_decr_checksum_unnecessary(struct sk_buff *skb)
  3153. {
  3154. if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  3155. if (skb->csum_level == 0)
  3156. skb->ip_summed = CHECKSUM_NONE;
  3157. else
  3158. skb->csum_level--;
  3159. }
  3160. }
  3161. static inline void __skb_incr_checksum_unnecessary(struct sk_buff *skb)
  3162. {
  3163. if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  3164. if (skb->csum_level < SKB_MAX_CSUM_LEVEL)
  3165. skb->csum_level++;
  3166. } else if (skb->ip_summed == CHECKSUM_NONE) {
  3167. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3168. skb->csum_level = 0;
  3169. }
  3170. }
  3171. /* Check if we need to perform checksum complete validation.
  3172. *
  3173. * Returns true if checksum complete is needed, false otherwise
  3174. * (either checksum is unnecessary or zero checksum is allowed).
  3175. */
  3176. static inline bool __skb_checksum_validate_needed(struct sk_buff *skb,
  3177. bool zero_okay,
  3178. __sum16 check)
  3179. {
  3180. if (skb_csum_unnecessary(skb) || (zero_okay && !check)) {
  3181. skb->csum_valid = 1;
  3182. __skb_decr_checksum_unnecessary(skb);
  3183. return false;
  3184. }
  3185. return true;
  3186. }
  3187. /* For small packets <= CHECKSUM_BREAK peform checksum complete directly
  3188. * in checksum_init.
  3189. */
  3190. #define CHECKSUM_BREAK 76
  3191. /* Unset checksum-complete
  3192. *
  3193. * Unset checksum complete can be done when packet is being modified
  3194. * (uncompressed for instance) and checksum-complete value is
  3195. * invalidated.
  3196. */
  3197. static inline void skb_checksum_complete_unset(struct sk_buff *skb)
  3198. {
  3199. if (skb->ip_summed == CHECKSUM_COMPLETE)
  3200. skb->ip_summed = CHECKSUM_NONE;
  3201. }
  3202. /* Validate (init) checksum based on checksum complete.
  3203. *
  3204. * Return values:
  3205. * 0: checksum is validated or try to in skb_checksum_complete. In the latter
  3206. * case the ip_summed will not be CHECKSUM_UNNECESSARY and the pseudo
  3207. * checksum is stored in skb->csum for use in __skb_checksum_complete
  3208. * non-zero: value of invalid checksum
  3209. *
  3210. */
  3211. static inline __sum16 __skb_checksum_validate_complete(struct sk_buff *skb,
  3212. bool complete,
  3213. __wsum psum)
  3214. {
  3215. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  3216. if (!csum_fold(csum_add(psum, skb->csum))) {
  3217. skb->csum_valid = 1;
  3218. return 0;
  3219. }
  3220. }
  3221. skb->csum = psum;
  3222. if (complete || skb->len <= CHECKSUM_BREAK) {
  3223. __sum16 csum;
  3224. csum = __skb_checksum_complete(skb);
  3225. skb->csum_valid = !csum;
  3226. return csum;
  3227. }
  3228. return 0;
  3229. }
  3230. static inline __wsum null_compute_pseudo(struct sk_buff *skb, int proto)
  3231. {
  3232. return 0;
  3233. }
  3234. /* Perform checksum validate (init). Note that this is a macro since we only
  3235. * want to calculate the pseudo header which is an input function if necessary.
  3236. * First we try to validate without any computation (checksum unnecessary) and
  3237. * then calculate based on checksum complete calling the function to compute
  3238. * pseudo header.
  3239. *
  3240. * Return values:
  3241. * 0: checksum is validated or try to in skb_checksum_complete
  3242. * non-zero: value of invalid checksum
  3243. */
  3244. #define __skb_checksum_validate(skb, proto, complete, \
  3245. zero_okay, check, compute_pseudo) \
  3246. ({ \
  3247. __sum16 __ret = 0; \
  3248. skb->csum_valid = 0; \
  3249. if (__skb_checksum_validate_needed(skb, zero_okay, check)) \
  3250. __ret = __skb_checksum_validate_complete(skb, \
  3251. complete, compute_pseudo(skb, proto)); \
  3252. __ret; \
  3253. })
  3254. #define skb_checksum_init(skb, proto, compute_pseudo) \
  3255. __skb_checksum_validate(skb, proto, false, false, 0, compute_pseudo)
  3256. #define skb_checksum_init_zero_check(skb, proto, check, compute_pseudo) \
  3257. __skb_checksum_validate(skb, proto, false, true, check, compute_pseudo)
  3258. #define skb_checksum_validate(skb, proto, compute_pseudo) \
  3259. __skb_checksum_validate(skb, proto, true, false, 0, compute_pseudo)
  3260. #define skb_checksum_validate_zero_check(skb, proto, check, \
  3261. compute_pseudo) \
  3262. __skb_checksum_validate(skb, proto, true, true, check, compute_pseudo)
  3263. #define skb_checksum_simple_validate(skb) \
  3264. __skb_checksum_validate(skb, 0, true, false, 0, null_compute_pseudo)
  3265. static inline bool __skb_checksum_convert_check(struct sk_buff *skb)
  3266. {
  3267. return (skb->ip_summed == CHECKSUM_NONE && skb->csum_valid);
  3268. }
  3269. static inline void __skb_checksum_convert(struct sk_buff *skb,
  3270. __sum16 check, __wsum pseudo)
  3271. {
  3272. skb->csum = ~pseudo;
  3273. skb->ip_summed = CHECKSUM_COMPLETE;
  3274. }
  3275. #define skb_checksum_try_convert(skb, proto, check, compute_pseudo) \
  3276. do { \
  3277. if (__skb_checksum_convert_check(skb)) \
  3278. __skb_checksum_convert(skb, check, \
  3279. compute_pseudo(skb, proto)); \
  3280. } while (0)
  3281. static inline void skb_remcsum_adjust_partial(struct sk_buff *skb, void *ptr,
  3282. u16 start, u16 offset)
  3283. {
  3284. skb->ip_summed = CHECKSUM_PARTIAL;
  3285. skb->csum_start = ((unsigned char *)ptr + start) - skb->head;
  3286. skb->csum_offset = offset - start;
  3287. }
  3288. /* Update skbuf and packet to reflect the remote checksum offload operation.
  3289. * When called, ptr indicates the starting point for skb->csum when
  3290. * ip_summed is CHECKSUM_COMPLETE. If we need create checksum complete
  3291. * here, skb_postpull_rcsum is done so skb->csum start is ptr.
  3292. */
  3293. static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr,
  3294. int start, int offset, bool nopartial)
  3295. {
  3296. __wsum delta;
  3297. if (!nopartial) {
  3298. skb_remcsum_adjust_partial(skb, ptr, start, offset);
  3299. return;
  3300. }
  3301. if (unlikely(skb->ip_summed != CHECKSUM_COMPLETE)) {
  3302. __skb_checksum_complete(skb);
  3303. skb_postpull_rcsum(skb, skb->data, ptr - (void *)skb->data);
  3304. }
  3305. delta = remcsum_adjust(ptr, skb->csum, start, offset);
  3306. /* Adjust skb->csum since we changed the packet */
  3307. skb->csum = csum_add(skb->csum, delta);
  3308. }
  3309. static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb)
  3310. {
  3311. #if IS_ENABLED(CONFIG_NF_CONNTRACK)
  3312. return (void *)(skb->_nfct & SKB_NFCT_PTRMASK);
  3313. #else
  3314. return NULL;
  3315. #endif
  3316. }
  3317. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  3318. void nf_conntrack_destroy(struct nf_conntrack *nfct);
  3319. static inline void nf_conntrack_put(struct nf_conntrack *nfct)
  3320. {
  3321. if (nfct && atomic_dec_and_test(&nfct->use))
  3322. nf_conntrack_destroy(nfct);
  3323. }
  3324. static inline void nf_conntrack_get(struct nf_conntrack *nfct)
  3325. {
  3326. if (nfct)
  3327. atomic_inc(&nfct->use);
  3328. }
  3329. #endif
  3330. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  3331. static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
  3332. {
  3333. if (nf_bridge && refcount_dec_and_test(&nf_bridge->use))
  3334. kfree(nf_bridge);
  3335. }
  3336. static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
  3337. {
  3338. if (nf_bridge)
  3339. refcount_inc(&nf_bridge->use);
  3340. }
  3341. #endif /* CONFIG_BRIDGE_NETFILTER */
  3342. static inline void nf_reset(struct sk_buff *skb)
  3343. {
  3344. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  3345. nf_conntrack_put(skb_nfct(skb));
  3346. skb->_nfct = 0;
  3347. #endif
  3348. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  3349. nf_bridge_put(skb->nf_bridge);
  3350. skb->nf_bridge = NULL;
  3351. #endif
  3352. }
  3353. static inline void nf_reset_trace(struct sk_buff *skb)
  3354. {
  3355. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
  3356. skb->nf_trace = 0;
  3357. #endif
  3358. }
  3359. /* Note: This doesn't put any conntrack and bridge info in dst. */
  3360. static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src,
  3361. bool copy)
  3362. {
  3363. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  3364. dst->_nfct = src->_nfct;
  3365. nf_conntrack_get(skb_nfct(src));
  3366. #endif
  3367. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  3368. dst->nf_bridge = src->nf_bridge;
  3369. nf_bridge_get(src->nf_bridge);
  3370. #endif
  3371. #if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) || defined(CONFIG_NF_TABLES)
  3372. if (copy)
  3373. dst->nf_trace = src->nf_trace;
  3374. #endif
  3375. }
  3376. static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  3377. {
  3378. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  3379. nf_conntrack_put(skb_nfct(dst));
  3380. #endif
  3381. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  3382. nf_bridge_put(dst->nf_bridge);
  3383. #endif
  3384. __nf_copy(dst, src, true);
  3385. }
  3386. #ifdef CONFIG_NETWORK_SECMARK
  3387. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  3388. {
  3389. to->secmark = from->secmark;
  3390. }
  3391. static inline void skb_init_secmark(struct sk_buff *skb)
  3392. {
  3393. skb->secmark = 0;
  3394. }
  3395. #else
  3396. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  3397. { }
  3398. static inline void skb_init_secmark(struct sk_buff *skb)
  3399. { }
  3400. #endif
  3401. static inline bool skb_irq_freeable(const struct sk_buff *skb)
  3402. {
  3403. return !skb->destructor &&
  3404. #if IS_ENABLED(CONFIG_XFRM)
  3405. !skb->sp &&
  3406. #endif
  3407. !skb_nfct(skb) &&
  3408. !skb->_skb_refdst &&
  3409. !skb_has_frag_list(skb);
  3410. }
  3411. static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
  3412. {
  3413. skb->queue_mapping = queue_mapping;
  3414. }
  3415. static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
  3416. {
  3417. return skb->queue_mapping;
  3418. }
  3419. static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
  3420. {
  3421. to->queue_mapping = from->queue_mapping;
  3422. }
  3423. static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
  3424. {
  3425. skb->queue_mapping = rx_queue + 1;
  3426. }
  3427. static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
  3428. {
  3429. return skb->queue_mapping - 1;
  3430. }
  3431. static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
  3432. {
  3433. return skb->queue_mapping != 0;
  3434. }
  3435. static inline void skb_set_dst_pending_confirm(struct sk_buff *skb, u32 val)
  3436. {
  3437. skb->dst_pending_confirm = val;
  3438. }
  3439. static inline bool skb_get_dst_pending_confirm(const struct sk_buff *skb)
  3440. {
  3441. return skb->dst_pending_confirm != 0;
  3442. }
  3443. static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
  3444. {
  3445. #ifdef CONFIG_XFRM
  3446. return skb->sp;
  3447. #else
  3448. return NULL;
  3449. #endif
  3450. }
  3451. /* Keeps track of mac header offset relative to skb->head.
  3452. * It is useful for TSO of Tunneling protocol. e.g. GRE.
  3453. * For non-tunnel skb it points to skb_mac_header() and for
  3454. * tunnel skb it points to outer mac header.
  3455. * Keeps track of level of encapsulation of network headers.
  3456. */
  3457. struct skb_gso_cb {
  3458. union {
  3459. int mac_offset;
  3460. int data_offset;
  3461. };
  3462. int encap_level;
  3463. __wsum csum;
  3464. __u16 csum_start;
  3465. };
  3466. #define SKB_SGO_CB_OFFSET 32
  3467. #define SKB_GSO_CB(skb) ((struct skb_gso_cb *)((skb)->cb + SKB_SGO_CB_OFFSET))
  3468. static inline int skb_tnl_header_len(const struct sk_buff *inner_skb)
  3469. {
  3470. return (skb_mac_header(inner_skb) - inner_skb->head) -
  3471. SKB_GSO_CB(inner_skb)->mac_offset;
  3472. }
  3473. static inline int gso_pskb_expand_head(struct sk_buff *skb, int extra)
  3474. {
  3475. int new_headroom, headroom;
  3476. int ret;
  3477. headroom = skb_headroom(skb);
  3478. ret = pskb_expand_head(skb, extra, 0, GFP_ATOMIC);
  3479. if (ret)
  3480. return ret;
  3481. new_headroom = skb_headroom(skb);
  3482. SKB_GSO_CB(skb)->mac_offset += (new_headroom - headroom);
  3483. return 0;
  3484. }
  3485. static inline void gso_reset_checksum(struct sk_buff *skb, __wsum res)
  3486. {
  3487. /* Do not update partial checksums if remote checksum is enabled. */
  3488. if (skb->remcsum_offload)
  3489. return;
  3490. SKB_GSO_CB(skb)->csum = res;
  3491. SKB_GSO_CB(skb)->csum_start = skb_checksum_start(skb) - skb->head;
  3492. }
  3493. /* Compute the checksum for a gso segment. First compute the checksum value
  3494. * from the start of transport header to SKB_GSO_CB(skb)->csum_start, and
  3495. * then add in skb->csum (checksum from csum_start to end of packet).
  3496. * skb->csum and csum_start are then updated to reflect the checksum of the
  3497. * resultant packet starting from the transport header-- the resultant checksum
  3498. * is in the res argument (i.e. normally zero or ~ of checksum of a pseudo
  3499. * header.
  3500. */
  3501. static inline __sum16 gso_make_checksum(struct sk_buff *skb, __wsum res)
  3502. {
  3503. unsigned char *csum_start = skb_transport_header(skb);
  3504. int plen = (skb->head + SKB_GSO_CB(skb)->csum_start) - csum_start;
  3505. __wsum partial = SKB_GSO_CB(skb)->csum;
  3506. SKB_GSO_CB(skb)->csum = res;
  3507. SKB_GSO_CB(skb)->csum_start = csum_start - skb->head;
  3508. return csum_fold(csum_partial(csum_start, plen, partial));
  3509. }
  3510. static inline bool skb_is_gso(const struct sk_buff *skb)
  3511. {
  3512. return skb_shinfo(skb)->gso_size;
  3513. }
  3514. /* Note: Should be called only if skb_is_gso(skb) is true */
  3515. static inline bool skb_is_gso_v6(const struct sk_buff *skb)
  3516. {
  3517. return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
  3518. }
  3519. static inline void skb_gso_reset(struct sk_buff *skb)
  3520. {
  3521. skb_shinfo(skb)->gso_size = 0;
  3522. skb_shinfo(skb)->gso_segs = 0;
  3523. skb_shinfo(skb)->gso_type = 0;
  3524. }
  3525. void __skb_warn_lro_forwarding(const struct sk_buff *skb);
  3526. static inline bool skb_warn_if_lro(const struct sk_buff *skb)
  3527. {
  3528. /* LRO sets gso_size but not gso_type, whereas if GSO is really
  3529. * wanted then gso_type will be set. */
  3530. const struct skb_shared_info *shinfo = skb_shinfo(skb);
  3531. if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
  3532. unlikely(shinfo->gso_type == 0)) {
  3533. __skb_warn_lro_forwarding(skb);
  3534. return true;
  3535. }
  3536. return false;
  3537. }
  3538. static inline void skb_forward_csum(struct sk_buff *skb)
  3539. {
  3540. /* Unfortunately we don't support this one. Any brave souls? */
  3541. if (skb->ip_summed == CHECKSUM_COMPLETE)
  3542. skb->ip_summed = CHECKSUM_NONE;
  3543. }
  3544. /**
  3545. * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
  3546. * @skb: skb to check
  3547. *
  3548. * fresh skbs have their ip_summed set to CHECKSUM_NONE.
  3549. * Instead of forcing ip_summed to CHECKSUM_NONE, we can
  3550. * use this helper, to document places where we make this assertion.
  3551. */
  3552. static inline void skb_checksum_none_assert(const struct sk_buff *skb)
  3553. {
  3554. #ifdef DEBUG
  3555. BUG_ON(skb->ip_summed != CHECKSUM_NONE);
  3556. #endif
  3557. }
  3558. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
  3559. int skb_checksum_setup(struct sk_buff *skb, bool recalculate);
  3560. struct sk_buff *skb_checksum_trimmed(struct sk_buff *skb,
  3561. unsigned int transport_len,
  3562. __sum16(*skb_chkf)(struct sk_buff *skb));
  3563. /**
  3564. * skb_head_is_locked - Determine if the skb->head is locked down
  3565. * @skb: skb to check
  3566. *
  3567. * The head on skbs build around a head frag can be removed if they are
  3568. * not cloned. This function returns true if the skb head is locked down
  3569. * due to either being allocated via kmalloc, or by being a clone with
  3570. * multiple references to the head.
  3571. */
  3572. static inline bool skb_head_is_locked(const struct sk_buff *skb)
  3573. {
  3574. return !skb->head_frag || skb_cloned(skb);
  3575. }
  3576. /**
  3577. * skb_gso_network_seglen - Return length of individual segments of a gso packet
  3578. *
  3579. * @skb: GSO skb
  3580. *
  3581. * skb_gso_network_seglen is used to determine the real size of the
  3582. * individual segments, including Layer3 (IP, IPv6) and L4 headers (TCP/UDP).
  3583. *
  3584. * The MAC/L2 header is not accounted for.
  3585. */
  3586. static inline unsigned int skb_gso_network_seglen(const struct sk_buff *skb)
  3587. {
  3588. unsigned int hdr_len = skb_transport_header(skb) -
  3589. skb_network_header(skb);
  3590. return hdr_len + skb_gso_transport_seglen(skb);
  3591. }
  3592. /* Local Checksum Offload.
  3593. * Compute outer checksum based on the assumption that the
  3594. * inner checksum will be offloaded later.
  3595. * See Documentation/networking/checksum-offloads.txt for
  3596. * explanation of how this works.
  3597. * Fill in outer checksum adjustment (e.g. with sum of outer
  3598. * pseudo-header) before calling.
  3599. * Also ensure that inner checksum is in linear data area.
  3600. */
  3601. static inline __wsum lco_csum(struct sk_buff *skb)
  3602. {
  3603. unsigned char *csum_start = skb_checksum_start(skb);
  3604. unsigned char *l4_hdr = skb_transport_header(skb);
  3605. __wsum partial;
  3606. /* Start with complement of inner checksum adjustment */
  3607. partial = ~csum_unfold(*(__force __sum16 *)(csum_start +
  3608. skb->csum_offset));
  3609. /* Add in checksum of our headers (incl. outer checksum
  3610. * adjustment filled in by caller) and return result.
  3611. */
  3612. return csum_partial(l4_hdr, csum_start - l4_hdr, partial);
  3613. }
  3614. #endif /* __KERNEL__ */
  3615. #endif /* _LINUX_SKBUFF_H */