skbuff.h 111 KB

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