skbuff.h 110 KB

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