skbuff.h 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265
  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/cache.h>
  20. #include <asm/atomic.h>
  21. #include <asm/types.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/net.h>
  24. #include <linux/textsearch.h>
  25. #include <net/checksum.h>
  26. #include <linux/rcupdate.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/hrtimer.h>
  29. /* Don't change this without changing skb_csum_unnecessary! */
  30. #define CHECKSUM_NONE 0
  31. #define CHECKSUM_UNNECESSARY 1
  32. #define CHECKSUM_COMPLETE 2
  33. #define CHECKSUM_PARTIAL 3
  34. #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
  35. ~(SMP_CACHE_BYTES - 1))
  36. #define SKB_WITH_OVERHEAD(X) \
  37. ((X) - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
  38. #define SKB_MAX_ORDER(X, ORDER) \
  39. SKB_WITH_OVERHEAD((PAGE_SIZE << (ORDER)) - (X))
  40. #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
  41. #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
  42. /* A. Checksumming of received packets by device.
  43. *
  44. * NONE: device failed to checksum this packet.
  45. * skb->csum is undefined.
  46. *
  47. * UNNECESSARY: device parsed packet and wouldbe verified checksum.
  48. * skb->csum is undefined.
  49. * It is bad option, but, unfortunately, many of vendors do this.
  50. * Apparently with secret goal to sell you new device, when you
  51. * will add new protocol to your host. F.e. IPv6. 8)
  52. *
  53. * COMPLETE: the most generic way. Device supplied checksum of _all_
  54. * the packet as seen by netif_rx in skb->csum.
  55. * NOTE: Even if device supports only some protocols, but
  56. * is able to produce some skb->csum, it MUST use COMPLETE,
  57. * not UNNECESSARY.
  58. *
  59. * PARTIAL: identical to the case for output below. This may occur
  60. * on a packet received directly from another Linux OS, e.g.,
  61. * a virtualised Linux kernel on the same host. The packet can
  62. * be treated in the same way as UNNECESSARY except that on
  63. * output (i.e., forwarding) the checksum must be filled in
  64. * by the OS or the hardware.
  65. *
  66. * B. Checksumming on output.
  67. *
  68. * NONE: skb is checksummed by protocol or csum is not required.
  69. *
  70. * PARTIAL: device is required to csum packet as seen by hard_start_xmit
  71. * from skb->csum_start to the end and to record the checksum
  72. * at skb->csum_start + skb->csum_offset.
  73. *
  74. * Device must show its capabilities in dev->features, set
  75. * at device setup time.
  76. * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
  77. * everything.
  78. * NETIF_F_NO_CSUM - loopback or reliable single hop media.
  79. * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
  80. * TCP/UDP over IPv4. Sigh. Vendors like this
  81. * way by an unknown reason. Though, see comment above
  82. * about CHECKSUM_UNNECESSARY. 8)
  83. * NETIF_F_IPV6_CSUM about as dumb as the last one but does IPv6 instead.
  84. *
  85. * Any questions? No questions, good. --ANK
  86. */
  87. struct net_device;
  88. struct scatterlist;
  89. struct pipe_inode_info;
  90. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  91. struct nf_conntrack {
  92. atomic_t use;
  93. };
  94. #endif
  95. #ifdef CONFIG_BRIDGE_NETFILTER
  96. struct nf_bridge_info {
  97. atomic_t use;
  98. struct net_device *physindev;
  99. struct net_device *physoutdev;
  100. unsigned int mask;
  101. unsigned long data[32 / sizeof(unsigned long)];
  102. };
  103. #endif
  104. struct sk_buff_head {
  105. /* These two members must be first. */
  106. struct sk_buff *next;
  107. struct sk_buff *prev;
  108. __u32 qlen;
  109. spinlock_t lock;
  110. };
  111. struct sk_buff;
  112. /* To allow 64K frame to be packed as single skb without frag_list. Since
  113. * GRO uses frags we allocate at least 16 regardless of page size.
  114. */
  115. #if (65536/PAGE_SIZE + 2) < 16
  116. #define MAX_SKB_FRAGS 16UL
  117. #else
  118. #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
  119. #endif
  120. typedef struct skb_frag_struct skb_frag_t;
  121. struct skb_frag_struct {
  122. struct page *page;
  123. #if (BITS_PER_LONG > 32) || (PAGE_SIZE >= 65536)
  124. __u32 page_offset;
  125. __u32 size;
  126. #else
  127. __u16 page_offset;
  128. __u16 size;
  129. #endif
  130. };
  131. #define HAVE_HW_TIME_STAMP
  132. /**
  133. * struct skb_shared_hwtstamps - hardware time stamps
  134. * @hwtstamp: hardware time stamp transformed into duration
  135. * since arbitrary point in time
  136. * @syststamp: hwtstamp transformed to system time base
  137. *
  138. * Software time stamps generated by ktime_get_real() are stored in
  139. * skb->tstamp. The relation between the different kinds of time
  140. * stamps is as follows:
  141. *
  142. * syststamp and tstamp can be compared against each other in
  143. * arbitrary combinations. The accuracy of a
  144. * syststamp/tstamp/"syststamp from other device" comparison is
  145. * limited by the accuracy of the transformation into system time
  146. * base. This depends on the device driver and its underlying
  147. * hardware.
  148. *
  149. * hwtstamps can only be compared against other hwtstamps from
  150. * the same device.
  151. *
  152. * This structure is attached to packets as part of the
  153. * &skb_shared_info. Use skb_hwtstamps() to get a pointer.
  154. */
  155. struct skb_shared_hwtstamps {
  156. ktime_t hwtstamp;
  157. ktime_t syststamp;
  158. };
  159. /* Definitions for tx_flags in struct skb_shared_info */
  160. enum {
  161. /* generate hardware time stamp */
  162. SKBTX_HW_TSTAMP = 1 << 0,
  163. /* generate software time stamp */
  164. SKBTX_SW_TSTAMP = 1 << 1,
  165. /* device driver is going to provide hardware time stamp */
  166. SKBTX_IN_PROGRESS = 1 << 2,
  167. /* ensure the originating sk reference is available on driver level */
  168. SKBTX_DRV_NEEDS_SK_REF = 1 << 3,
  169. };
  170. /* This data is invariant across clones and lives at
  171. * the end of the header data, ie. at skb->end.
  172. */
  173. struct skb_shared_info {
  174. unsigned short nr_frags;
  175. unsigned short gso_size;
  176. /* Warning: this field is not always filled in (UFO)! */
  177. unsigned short gso_segs;
  178. unsigned short gso_type;
  179. __be32 ip6_frag_id;
  180. __u8 tx_flags;
  181. struct sk_buff *frag_list;
  182. struct skb_shared_hwtstamps hwtstamps;
  183. /*
  184. * Warning : all fields before dataref are cleared in __alloc_skb()
  185. */
  186. atomic_t dataref;
  187. /* Intermediate layers must ensure that destructor_arg
  188. * remains valid until skb destructor */
  189. void * destructor_arg;
  190. /* must be last field, see pskb_expand_head() */
  191. skb_frag_t frags[MAX_SKB_FRAGS];
  192. };
  193. /* We divide dataref into two halves. The higher 16 bits hold references
  194. * to the payload part of skb->data. The lower 16 bits hold references to
  195. * the entire skb->data. A clone of a headerless skb holds the length of
  196. * the header in skb->hdr_len.
  197. *
  198. * All users must obey the rule that the skb->data reference count must be
  199. * greater than or equal to the payload reference count.
  200. *
  201. * Holding a reference to the payload part means that the user does not
  202. * care about modifications to the header part of skb->data.
  203. */
  204. #define SKB_DATAREF_SHIFT 16
  205. #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
  206. enum {
  207. SKB_FCLONE_UNAVAILABLE,
  208. SKB_FCLONE_ORIG,
  209. SKB_FCLONE_CLONE,
  210. };
  211. enum {
  212. SKB_GSO_TCPV4 = 1 << 0,
  213. SKB_GSO_UDP = 1 << 1,
  214. /* This indicates the skb is from an untrusted source. */
  215. SKB_GSO_DODGY = 1 << 2,
  216. /* This indicates the tcp segment has CWR set. */
  217. SKB_GSO_TCP_ECN = 1 << 3,
  218. SKB_GSO_TCPV6 = 1 << 4,
  219. SKB_GSO_FCOE = 1 << 5,
  220. };
  221. #if BITS_PER_LONG > 32
  222. #define NET_SKBUFF_DATA_USES_OFFSET 1
  223. #endif
  224. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  225. typedef unsigned int sk_buff_data_t;
  226. #else
  227. typedef unsigned char *sk_buff_data_t;
  228. #endif
  229. #if defined(CONFIG_NF_DEFRAG_IPV4) || defined(CONFIG_NF_DEFRAG_IPV4_MODULE) || \
  230. defined(CONFIG_NF_DEFRAG_IPV6) || defined(CONFIG_NF_DEFRAG_IPV6_MODULE)
  231. #define NET_SKBUFF_NF_DEFRAG_NEEDED 1
  232. #endif
  233. /**
  234. * struct sk_buff - socket buffer
  235. * @next: Next buffer in list
  236. * @prev: Previous buffer in list
  237. * @sk: Socket we are owned by
  238. * @tstamp: Time we arrived
  239. * @dev: Device we arrived on/are leaving by
  240. * @transport_header: Transport layer header
  241. * @network_header: Network layer header
  242. * @mac_header: Link layer header
  243. * @_skb_refdst: destination entry (with norefcount bit)
  244. * @sp: the security path, used for xfrm
  245. * @cb: Control buffer. Free for use by every layer. Put private vars here
  246. * @len: Length of actual data
  247. * @data_len: Data length
  248. * @mac_len: Length of link layer header
  249. * @hdr_len: writable header length of cloned skb
  250. * @csum: Checksum (must include start/offset pair)
  251. * @csum_start: Offset from skb->head where checksumming should start
  252. * @csum_offset: Offset from csum_start where checksum should be stored
  253. * @local_df: allow local fragmentation
  254. * @cloned: Head may be cloned (check refcnt to be sure)
  255. * @nohdr: Payload reference only, must not modify header
  256. * @pkt_type: Packet class
  257. * @fclone: skbuff clone status
  258. * @ip_summed: Driver fed us an IP checksum
  259. * @priority: Packet queueing priority
  260. * @users: User count - see {datagram,tcp}.c
  261. * @protocol: Packet protocol from driver
  262. * @truesize: Buffer size
  263. * @head: Head of buffer
  264. * @data: Data head pointer
  265. * @tail: Tail pointer
  266. * @end: End pointer
  267. * @destructor: Destruct function
  268. * @mark: Generic packet mark
  269. * @nfct: Associated connection, if any
  270. * @ipvs_property: skbuff is owned by ipvs
  271. * @peeked: this packet has been seen already, so stats have been
  272. * done for it, don't do them again
  273. * @nf_trace: netfilter packet trace flag
  274. * @nfctinfo: Relationship of this skb to the connection
  275. * @nfct_reasm: netfilter conntrack re-assembly pointer
  276. * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
  277. * @skb_iif: ifindex of device we arrived on
  278. * @rxhash: the packet hash computed on receive
  279. * @queue_mapping: Queue mapping for multiqueue devices
  280. * @tc_index: Traffic control index
  281. * @tc_verd: traffic control verdict
  282. * @ndisc_nodetype: router type (from link layer)
  283. * @dma_cookie: a cookie to one of several possible DMA operations
  284. * done by skb DMA functions
  285. * @secmark: security marking
  286. * @vlan_tci: vlan tag control information
  287. */
  288. struct sk_buff {
  289. /* These two members must be first. */
  290. struct sk_buff *next;
  291. struct sk_buff *prev;
  292. ktime_t tstamp;
  293. struct sock *sk;
  294. struct net_device *dev;
  295. /*
  296. * This is the control buffer. It is free to use for every
  297. * layer. Please put your private variables there. If you
  298. * want to keep them across layers you have to do a skb_clone()
  299. * first. This is owned by whoever has the skb queued ATM.
  300. */
  301. char cb[48] __aligned(8);
  302. unsigned long _skb_refdst;
  303. #ifdef CONFIG_XFRM
  304. struct sec_path *sp;
  305. #endif
  306. unsigned int len,
  307. data_len;
  308. __u16 mac_len,
  309. hdr_len;
  310. union {
  311. __wsum csum;
  312. struct {
  313. __u16 csum_start;
  314. __u16 csum_offset;
  315. };
  316. };
  317. __u32 priority;
  318. kmemcheck_bitfield_begin(flags1);
  319. __u8 local_df:1,
  320. cloned:1,
  321. ip_summed:2,
  322. nohdr:1,
  323. nfctinfo:3;
  324. __u8 pkt_type:3,
  325. fclone:2,
  326. ipvs_property:1,
  327. peeked:1,
  328. nf_trace:1;
  329. kmemcheck_bitfield_end(flags1);
  330. __be16 protocol;
  331. void (*destructor)(struct sk_buff *skb);
  332. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  333. struct nf_conntrack *nfct;
  334. #endif
  335. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  336. struct sk_buff *nfct_reasm;
  337. #endif
  338. #ifdef CONFIG_BRIDGE_NETFILTER
  339. struct nf_bridge_info *nf_bridge;
  340. #endif
  341. int skb_iif;
  342. #ifdef CONFIG_NET_SCHED
  343. __u16 tc_index; /* traffic control index */
  344. #ifdef CONFIG_NET_CLS_ACT
  345. __u16 tc_verd; /* traffic control verdict */
  346. #endif
  347. #endif
  348. __u32 rxhash;
  349. __u16 queue_mapping;
  350. kmemcheck_bitfield_begin(flags2);
  351. #ifdef CONFIG_IPV6_NDISC_NODETYPE
  352. __u8 ndisc_nodetype:2;
  353. #endif
  354. __u8 ooo_okay:1;
  355. kmemcheck_bitfield_end(flags2);
  356. /* 0/13 bit hole */
  357. #ifdef CONFIG_NET_DMA
  358. dma_cookie_t dma_cookie;
  359. #endif
  360. #ifdef CONFIG_NETWORK_SECMARK
  361. __u32 secmark;
  362. #endif
  363. union {
  364. __u32 mark;
  365. __u32 dropcount;
  366. };
  367. __u16 vlan_tci;
  368. sk_buff_data_t transport_header;
  369. sk_buff_data_t network_header;
  370. sk_buff_data_t mac_header;
  371. /* These elements must be at the end, see alloc_skb() for details. */
  372. sk_buff_data_t tail;
  373. sk_buff_data_t end;
  374. unsigned char *head,
  375. *data;
  376. unsigned int truesize;
  377. atomic_t users;
  378. };
  379. #ifdef __KERNEL__
  380. /*
  381. * Handling routines are only of interest to the kernel
  382. */
  383. #include <linux/slab.h>
  384. #include <asm/system.h>
  385. /*
  386. * skb might have a dst pointer attached, refcounted or not.
  387. * _skb_refdst low order bit is set if refcount was _not_ taken
  388. */
  389. #define SKB_DST_NOREF 1UL
  390. #define SKB_DST_PTRMASK ~(SKB_DST_NOREF)
  391. /**
  392. * skb_dst - returns skb dst_entry
  393. * @skb: buffer
  394. *
  395. * Returns skb dst_entry, regardless of reference taken or not.
  396. */
  397. static inline struct dst_entry *skb_dst(const struct sk_buff *skb)
  398. {
  399. /* If refdst was not refcounted, check we still are in a
  400. * rcu_read_lock section
  401. */
  402. WARN_ON((skb->_skb_refdst & SKB_DST_NOREF) &&
  403. !rcu_read_lock_held() &&
  404. !rcu_read_lock_bh_held());
  405. return (struct dst_entry *)(skb->_skb_refdst & SKB_DST_PTRMASK);
  406. }
  407. /**
  408. * skb_dst_set - sets skb dst
  409. * @skb: buffer
  410. * @dst: dst entry
  411. *
  412. * Sets skb dst, assuming a reference was taken on dst and should
  413. * be released by skb_dst_drop()
  414. */
  415. static inline void skb_dst_set(struct sk_buff *skb, struct dst_entry *dst)
  416. {
  417. skb->_skb_refdst = (unsigned long)dst;
  418. }
  419. extern void skb_dst_set_noref(struct sk_buff *skb, struct dst_entry *dst);
  420. /**
  421. * skb_dst_is_noref - Test if skb dst isn't refcounted
  422. * @skb: buffer
  423. */
  424. static inline bool skb_dst_is_noref(const struct sk_buff *skb)
  425. {
  426. return (skb->_skb_refdst & SKB_DST_NOREF) && skb_dst(skb);
  427. }
  428. static inline struct rtable *skb_rtable(const struct sk_buff *skb)
  429. {
  430. return (struct rtable *)skb_dst(skb);
  431. }
  432. extern void kfree_skb(struct sk_buff *skb);
  433. extern void consume_skb(struct sk_buff *skb);
  434. extern void __kfree_skb(struct sk_buff *skb);
  435. extern struct sk_buff *__alloc_skb(unsigned int size,
  436. gfp_t priority, int fclone, int node);
  437. static inline struct sk_buff *alloc_skb(unsigned int size,
  438. gfp_t priority)
  439. {
  440. return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
  441. }
  442. static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
  443. gfp_t priority)
  444. {
  445. return __alloc_skb(size, priority, 1, NUMA_NO_NODE);
  446. }
  447. extern bool skb_recycle_check(struct sk_buff *skb, int skb_size);
  448. extern struct sk_buff *skb_morph(struct sk_buff *dst, struct sk_buff *src);
  449. extern struct sk_buff *skb_clone(struct sk_buff *skb,
  450. gfp_t priority);
  451. extern struct sk_buff *skb_copy(const struct sk_buff *skb,
  452. gfp_t priority);
  453. extern struct sk_buff *pskb_copy(struct sk_buff *skb,
  454. gfp_t gfp_mask);
  455. extern int pskb_expand_head(struct sk_buff *skb,
  456. int nhead, int ntail,
  457. gfp_t gfp_mask);
  458. extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
  459. unsigned int headroom);
  460. extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  461. int newheadroom, int newtailroom,
  462. gfp_t priority);
  463. extern int skb_to_sgvec(struct sk_buff *skb,
  464. struct scatterlist *sg, int offset,
  465. int len);
  466. extern int skb_cow_data(struct sk_buff *skb, int tailbits,
  467. struct sk_buff **trailer);
  468. extern int skb_pad(struct sk_buff *skb, int pad);
  469. #define dev_kfree_skb(a) consume_skb(a)
  470. extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  471. int getfrag(void *from, char *to, int offset,
  472. int len,int odd, struct sk_buff *skb),
  473. void *from, int length);
  474. struct skb_seq_state {
  475. __u32 lower_offset;
  476. __u32 upper_offset;
  477. __u32 frag_idx;
  478. __u32 stepped_offset;
  479. struct sk_buff *root_skb;
  480. struct sk_buff *cur_skb;
  481. __u8 *frag_data;
  482. };
  483. extern void skb_prepare_seq_read(struct sk_buff *skb,
  484. unsigned int from, unsigned int to,
  485. struct skb_seq_state *st);
  486. extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  487. struct skb_seq_state *st);
  488. extern void skb_abort_seq_read(struct skb_seq_state *st);
  489. extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  490. unsigned int to, struct ts_config *config,
  491. struct ts_state *state);
  492. extern __u32 __skb_get_rxhash(struct sk_buff *skb);
  493. static inline __u32 skb_get_rxhash(struct sk_buff *skb)
  494. {
  495. if (!skb->rxhash)
  496. skb->rxhash = __skb_get_rxhash(skb);
  497. return skb->rxhash;
  498. }
  499. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  500. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  501. {
  502. return skb->head + skb->end;
  503. }
  504. #else
  505. static inline unsigned char *skb_end_pointer(const struct sk_buff *skb)
  506. {
  507. return skb->end;
  508. }
  509. #endif
  510. /* Internal */
  511. #define skb_shinfo(SKB) ((struct skb_shared_info *)(skb_end_pointer(SKB)))
  512. static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
  513. {
  514. return &skb_shinfo(skb)->hwtstamps;
  515. }
  516. /**
  517. * skb_queue_empty - check if a queue is empty
  518. * @list: queue head
  519. *
  520. * Returns true if the queue is empty, false otherwise.
  521. */
  522. static inline int skb_queue_empty(const struct sk_buff_head *list)
  523. {
  524. return list->next == (struct sk_buff *)list;
  525. }
  526. /**
  527. * skb_queue_is_last - check if skb is the last entry in the queue
  528. * @list: queue head
  529. * @skb: buffer
  530. *
  531. * Returns true if @skb is the last buffer on the list.
  532. */
  533. static inline bool skb_queue_is_last(const struct sk_buff_head *list,
  534. const struct sk_buff *skb)
  535. {
  536. return skb->next == (struct sk_buff *)list;
  537. }
  538. /**
  539. * skb_queue_is_first - check if skb is the first entry in the queue
  540. * @list: queue head
  541. * @skb: buffer
  542. *
  543. * Returns true if @skb is the first buffer on the list.
  544. */
  545. static inline bool skb_queue_is_first(const struct sk_buff_head *list,
  546. const struct sk_buff *skb)
  547. {
  548. return skb->prev == (struct sk_buff *)list;
  549. }
  550. /**
  551. * skb_queue_next - return the next packet in the queue
  552. * @list: queue head
  553. * @skb: current buffer
  554. *
  555. * Return the next packet in @list after @skb. It is only valid to
  556. * call this if skb_queue_is_last() evaluates to false.
  557. */
  558. static inline struct sk_buff *skb_queue_next(const struct sk_buff_head *list,
  559. const struct sk_buff *skb)
  560. {
  561. /* This BUG_ON may seem severe, but if we just return then we
  562. * are going to dereference garbage.
  563. */
  564. BUG_ON(skb_queue_is_last(list, skb));
  565. return skb->next;
  566. }
  567. /**
  568. * skb_queue_prev - return the prev packet in the queue
  569. * @list: queue head
  570. * @skb: current buffer
  571. *
  572. * Return the prev packet in @list before @skb. It is only valid to
  573. * call this if skb_queue_is_first() evaluates to false.
  574. */
  575. static inline struct sk_buff *skb_queue_prev(const struct sk_buff_head *list,
  576. const struct sk_buff *skb)
  577. {
  578. /* This BUG_ON may seem severe, but if we just return then we
  579. * are going to dereference garbage.
  580. */
  581. BUG_ON(skb_queue_is_first(list, skb));
  582. return skb->prev;
  583. }
  584. /**
  585. * skb_get - reference buffer
  586. * @skb: buffer to reference
  587. *
  588. * Makes another reference to a socket buffer and returns a pointer
  589. * to the buffer.
  590. */
  591. static inline struct sk_buff *skb_get(struct sk_buff *skb)
  592. {
  593. atomic_inc(&skb->users);
  594. return skb;
  595. }
  596. /*
  597. * If users == 1, we are the only owner and are can avoid redundant
  598. * atomic change.
  599. */
  600. /**
  601. * skb_cloned - is the buffer a clone
  602. * @skb: buffer to check
  603. *
  604. * Returns true if the buffer was generated with skb_clone() and is
  605. * one of multiple shared copies of the buffer. Cloned buffers are
  606. * shared data so must not be written to under normal circumstances.
  607. */
  608. static inline int skb_cloned(const struct sk_buff *skb)
  609. {
  610. return skb->cloned &&
  611. (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
  612. }
  613. /**
  614. * skb_header_cloned - is the header a clone
  615. * @skb: buffer to check
  616. *
  617. * Returns true if modifying the header part of the buffer requires
  618. * the data to be copied.
  619. */
  620. static inline int skb_header_cloned(const struct sk_buff *skb)
  621. {
  622. int dataref;
  623. if (!skb->cloned)
  624. return 0;
  625. dataref = atomic_read(&skb_shinfo(skb)->dataref);
  626. dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
  627. return dataref != 1;
  628. }
  629. /**
  630. * skb_header_release - release reference to header
  631. * @skb: buffer to operate on
  632. *
  633. * Drop a reference to the header part of the buffer. This is done
  634. * by acquiring a payload reference. You must not read from the header
  635. * part of skb->data after this.
  636. */
  637. static inline void skb_header_release(struct sk_buff *skb)
  638. {
  639. BUG_ON(skb->nohdr);
  640. skb->nohdr = 1;
  641. atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
  642. }
  643. /**
  644. * skb_shared - is the buffer shared
  645. * @skb: buffer to check
  646. *
  647. * Returns true if more than one person has a reference to this
  648. * buffer.
  649. */
  650. static inline int skb_shared(const struct sk_buff *skb)
  651. {
  652. return atomic_read(&skb->users) != 1;
  653. }
  654. /**
  655. * skb_share_check - check if buffer is shared and if so clone it
  656. * @skb: buffer to check
  657. * @pri: priority for memory allocation
  658. *
  659. * If the buffer is shared the buffer is cloned and the old copy
  660. * drops a reference. A new clone with a single reference is returned.
  661. * If the buffer is not shared the original buffer is returned. When
  662. * being called from interrupt status or with spinlocks held pri must
  663. * be GFP_ATOMIC.
  664. *
  665. * NULL is returned on a memory allocation failure.
  666. */
  667. static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
  668. gfp_t pri)
  669. {
  670. might_sleep_if(pri & __GFP_WAIT);
  671. if (skb_shared(skb)) {
  672. struct sk_buff *nskb = skb_clone(skb, pri);
  673. kfree_skb(skb);
  674. skb = nskb;
  675. }
  676. return skb;
  677. }
  678. /*
  679. * Copy shared buffers into a new sk_buff. We effectively do COW on
  680. * packets to handle cases where we have a local reader and forward
  681. * and a couple of other messy ones. The normal one is tcpdumping
  682. * a packet thats being forwarded.
  683. */
  684. /**
  685. * skb_unshare - make a copy of a shared buffer
  686. * @skb: buffer to check
  687. * @pri: priority for memory allocation
  688. *
  689. * If the socket buffer is a clone then this function creates a new
  690. * copy of the data, drops a reference count on the old copy and returns
  691. * the new copy with the reference count at 1. If the buffer is not a clone
  692. * the original buffer is returned. When called with a spinlock held or
  693. * from interrupt state @pri must be %GFP_ATOMIC
  694. *
  695. * %NULL is returned on a memory allocation failure.
  696. */
  697. static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
  698. gfp_t pri)
  699. {
  700. might_sleep_if(pri & __GFP_WAIT);
  701. if (skb_cloned(skb)) {
  702. struct sk_buff *nskb = skb_copy(skb, pri);
  703. kfree_skb(skb); /* Free our shared copy */
  704. skb = nskb;
  705. }
  706. return skb;
  707. }
  708. /**
  709. * skb_peek - peek at the head of an &sk_buff_head
  710. * @list_: list to peek at
  711. *
  712. * Peek an &sk_buff. Unlike most other operations you _MUST_
  713. * be careful with this one. A peek leaves the buffer on the
  714. * list and someone else may run off with it. You must hold
  715. * the appropriate locks or have a private queue to do this.
  716. *
  717. * Returns %NULL for an empty list or a pointer to the head element.
  718. * The reference count is not incremented and the reference is therefore
  719. * volatile. Use with caution.
  720. */
  721. static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
  722. {
  723. struct sk_buff *list = ((struct sk_buff *)list_)->next;
  724. if (list == (struct sk_buff *)list_)
  725. list = NULL;
  726. return list;
  727. }
  728. /**
  729. * skb_peek_tail - peek at the tail of an &sk_buff_head
  730. * @list_: list to peek at
  731. *
  732. * Peek an &sk_buff. Unlike most other operations you _MUST_
  733. * be careful with this one. A peek leaves the buffer on the
  734. * list and someone else may run off with it. You must hold
  735. * the appropriate locks or have a private queue to do this.
  736. *
  737. * Returns %NULL for an empty list or a pointer to the tail element.
  738. * The reference count is not incremented and the reference is therefore
  739. * volatile. Use with caution.
  740. */
  741. static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
  742. {
  743. struct sk_buff *list = ((struct sk_buff *)list_)->prev;
  744. if (list == (struct sk_buff *)list_)
  745. list = NULL;
  746. return list;
  747. }
  748. /**
  749. * skb_queue_len - get queue length
  750. * @list_: list to measure
  751. *
  752. * Return the length of an &sk_buff queue.
  753. */
  754. static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
  755. {
  756. return list_->qlen;
  757. }
  758. /**
  759. * __skb_queue_head_init - initialize non-spinlock portions of sk_buff_head
  760. * @list: queue to initialize
  761. *
  762. * This initializes only the list and queue length aspects of
  763. * an sk_buff_head object. This allows to initialize the list
  764. * aspects of an sk_buff_head without reinitializing things like
  765. * the spinlock. It can also be used for on-stack sk_buff_head
  766. * objects where the spinlock is known to not be used.
  767. */
  768. static inline void __skb_queue_head_init(struct sk_buff_head *list)
  769. {
  770. list->prev = list->next = (struct sk_buff *)list;
  771. list->qlen = 0;
  772. }
  773. /*
  774. * This function creates a split out lock class for each invocation;
  775. * this is needed for now since a whole lot of users of the skb-queue
  776. * infrastructure in drivers have different locking usage (in hardirq)
  777. * than the networking core (in softirq only). In the long run either the
  778. * network layer or drivers should need annotation to consolidate the
  779. * main types of usage into 3 classes.
  780. */
  781. static inline void skb_queue_head_init(struct sk_buff_head *list)
  782. {
  783. spin_lock_init(&list->lock);
  784. __skb_queue_head_init(list);
  785. }
  786. static inline void skb_queue_head_init_class(struct sk_buff_head *list,
  787. struct lock_class_key *class)
  788. {
  789. skb_queue_head_init(list);
  790. lockdep_set_class(&list->lock, class);
  791. }
  792. /*
  793. * Insert an sk_buff on a list.
  794. *
  795. * The "__skb_xxxx()" functions are the non-atomic ones that
  796. * can only be called with interrupts disabled.
  797. */
  798. extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
  799. static inline void __skb_insert(struct sk_buff *newsk,
  800. struct sk_buff *prev, struct sk_buff *next,
  801. struct sk_buff_head *list)
  802. {
  803. newsk->next = next;
  804. newsk->prev = prev;
  805. next->prev = prev->next = newsk;
  806. list->qlen++;
  807. }
  808. static inline void __skb_queue_splice(const struct sk_buff_head *list,
  809. struct sk_buff *prev,
  810. struct sk_buff *next)
  811. {
  812. struct sk_buff *first = list->next;
  813. struct sk_buff *last = list->prev;
  814. first->prev = prev;
  815. prev->next = first;
  816. last->next = next;
  817. next->prev = last;
  818. }
  819. /**
  820. * skb_queue_splice - join two skb lists, this is designed for stacks
  821. * @list: the new list to add
  822. * @head: the place to add it in the first list
  823. */
  824. static inline void skb_queue_splice(const struct sk_buff_head *list,
  825. struct sk_buff_head *head)
  826. {
  827. if (!skb_queue_empty(list)) {
  828. __skb_queue_splice(list, (struct sk_buff *) head, head->next);
  829. head->qlen += list->qlen;
  830. }
  831. }
  832. /**
  833. * skb_queue_splice - join two skb lists and reinitialise the emptied list
  834. * @list: the new list to add
  835. * @head: the place to add it in the first list
  836. *
  837. * The list at @list is reinitialised
  838. */
  839. static inline void skb_queue_splice_init(struct sk_buff_head *list,
  840. struct sk_buff_head *head)
  841. {
  842. if (!skb_queue_empty(list)) {
  843. __skb_queue_splice(list, (struct sk_buff *) head, head->next);
  844. head->qlen += list->qlen;
  845. __skb_queue_head_init(list);
  846. }
  847. }
  848. /**
  849. * skb_queue_splice_tail - join two skb lists, each list being a queue
  850. * @list: the new list to add
  851. * @head: the place to add it in the first list
  852. */
  853. static inline void skb_queue_splice_tail(const struct sk_buff_head *list,
  854. struct sk_buff_head *head)
  855. {
  856. if (!skb_queue_empty(list)) {
  857. __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
  858. head->qlen += list->qlen;
  859. }
  860. }
  861. /**
  862. * skb_queue_splice_tail - join two skb lists and reinitialise the emptied list
  863. * @list: the new list to add
  864. * @head: the place to add it in the first list
  865. *
  866. * Each of the lists is a queue.
  867. * The list at @list is reinitialised
  868. */
  869. static inline void skb_queue_splice_tail_init(struct sk_buff_head *list,
  870. struct sk_buff_head *head)
  871. {
  872. if (!skb_queue_empty(list)) {
  873. __skb_queue_splice(list, head->prev, (struct sk_buff *) head);
  874. head->qlen += list->qlen;
  875. __skb_queue_head_init(list);
  876. }
  877. }
  878. /**
  879. * __skb_queue_after - queue a buffer at the list head
  880. * @list: list to use
  881. * @prev: place after this buffer
  882. * @newsk: buffer to queue
  883. *
  884. * Queue a buffer int the middle of a list. This function takes no locks
  885. * and you must therefore hold required locks before calling it.
  886. *
  887. * A buffer cannot be placed on two lists at the same time.
  888. */
  889. static inline void __skb_queue_after(struct sk_buff_head *list,
  890. struct sk_buff *prev,
  891. struct sk_buff *newsk)
  892. {
  893. __skb_insert(newsk, prev, prev->next, list);
  894. }
  895. extern void skb_append(struct sk_buff *old, struct sk_buff *newsk,
  896. struct sk_buff_head *list);
  897. static inline void __skb_queue_before(struct sk_buff_head *list,
  898. struct sk_buff *next,
  899. struct sk_buff *newsk)
  900. {
  901. __skb_insert(newsk, next->prev, next, list);
  902. }
  903. /**
  904. * __skb_queue_head - queue a buffer at the list head
  905. * @list: list to use
  906. * @newsk: buffer to queue
  907. *
  908. * Queue a buffer at the start of a list. This function takes no locks
  909. * and you must therefore hold required locks before calling it.
  910. *
  911. * A buffer cannot be placed on two lists at the same time.
  912. */
  913. extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
  914. static inline void __skb_queue_head(struct sk_buff_head *list,
  915. struct sk_buff *newsk)
  916. {
  917. __skb_queue_after(list, (struct sk_buff *)list, newsk);
  918. }
  919. /**
  920. * __skb_queue_tail - queue a buffer at the list tail
  921. * @list: list to use
  922. * @newsk: buffer to queue
  923. *
  924. * Queue a buffer at the end of a list. This function takes no locks
  925. * and you must therefore hold required locks before calling it.
  926. *
  927. * A buffer cannot be placed on two lists at the same time.
  928. */
  929. extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
  930. static inline void __skb_queue_tail(struct sk_buff_head *list,
  931. struct sk_buff *newsk)
  932. {
  933. __skb_queue_before(list, (struct sk_buff *)list, newsk);
  934. }
  935. /*
  936. * remove sk_buff from list. _Must_ be called atomically, and with
  937. * the list known..
  938. */
  939. extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
  940. static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  941. {
  942. struct sk_buff *next, *prev;
  943. list->qlen--;
  944. next = skb->next;
  945. prev = skb->prev;
  946. skb->next = skb->prev = NULL;
  947. next->prev = prev;
  948. prev->next = next;
  949. }
  950. /**
  951. * __skb_dequeue - remove from the head of the queue
  952. * @list: list to dequeue from
  953. *
  954. * Remove the head of the list. This function does not take any locks
  955. * so must be used with appropriate locks held only. The head item is
  956. * returned or %NULL if the list is empty.
  957. */
  958. extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
  959. static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
  960. {
  961. struct sk_buff *skb = skb_peek(list);
  962. if (skb)
  963. __skb_unlink(skb, list);
  964. return skb;
  965. }
  966. /**
  967. * __skb_dequeue_tail - remove from the tail of the queue
  968. * @list: list to dequeue from
  969. *
  970. * Remove the tail of the list. This function does not take any locks
  971. * so must be used with appropriate locks held only. The tail item is
  972. * returned or %NULL if the list is empty.
  973. */
  974. extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
  975. static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
  976. {
  977. struct sk_buff *skb = skb_peek_tail(list);
  978. if (skb)
  979. __skb_unlink(skb, list);
  980. return skb;
  981. }
  982. static inline int skb_is_nonlinear(const struct sk_buff *skb)
  983. {
  984. return skb->data_len;
  985. }
  986. static inline unsigned int skb_headlen(const struct sk_buff *skb)
  987. {
  988. return skb->len - skb->data_len;
  989. }
  990. static inline int skb_pagelen(const struct sk_buff *skb)
  991. {
  992. int i, len = 0;
  993. for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
  994. len += skb_shinfo(skb)->frags[i].size;
  995. return len + skb_headlen(skb);
  996. }
  997. static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
  998. struct page *page, int off, int size)
  999. {
  1000. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1001. frag->page = page;
  1002. frag->page_offset = off;
  1003. frag->size = size;
  1004. skb_shinfo(skb)->nr_frags = i + 1;
  1005. }
  1006. extern void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page,
  1007. int off, int size);
  1008. #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
  1009. #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_has_frag_list(skb))
  1010. #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
  1011. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1012. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  1013. {
  1014. return skb->head + skb->tail;
  1015. }
  1016. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  1017. {
  1018. skb->tail = skb->data - skb->head;
  1019. }
  1020. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  1021. {
  1022. skb_reset_tail_pointer(skb);
  1023. skb->tail += offset;
  1024. }
  1025. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  1026. static inline unsigned char *skb_tail_pointer(const struct sk_buff *skb)
  1027. {
  1028. return skb->tail;
  1029. }
  1030. static inline void skb_reset_tail_pointer(struct sk_buff *skb)
  1031. {
  1032. skb->tail = skb->data;
  1033. }
  1034. static inline void skb_set_tail_pointer(struct sk_buff *skb, const int offset)
  1035. {
  1036. skb->tail = skb->data + offset;
  1037. }
  1038. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  1039. /*
  1040. * Add data to an sk_buff
  1041. */
  1042. extern unsigned char *skb_put(struct sk_buff *skb, unsigned int len);
  1043. static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
  1044. {
  1045. unsigned char *tmp = skb_tail_pointer(skb);
  1046. SKB_LINEAR_ASSERT(skb);
  1047. skb->tail += len;
  1048. skb->len += len;
  1049. return tmp;
  1050. }
  1051. extern unsigned char *skb_push(struct sk_buff *skb, unsigned int len);
  1052. static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
  1053. {
  1054. skb->data -= len;
  1055. skb->len += len;
  1056. return skb->data;
  1057. }
  1058. extern unsigned char *skb_pull(struct sk_buff *skb, unsigned int len);
  1059. static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
  1060. {
  1061. skb->len -= len;
  1062. BUG_ON(skb->len < skb->data_len);
  1063. return skb->data += len;
  1064. }
  1065. static inline unsigned char *skb_pull_inline(struct sk_buff *skb, unsigned int len)
  1066. {
  1067. return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
  1068. }
  1069. extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
  1070. static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
  1071. {
  1072. if (len > skb_headlen(skb) &&
  1073. !__pskb_pull_tail(skb, len - skb_headlen(skb)))
  1074. return NULL;
  1075. skb->len -= len;
  1076. return skb->data += len;
  1077. }
  1078. static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
  1079. {
  1080. return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
  1081. }
  1082. static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
  1083. {
  1084. if (likely(len <= skb_headlen(skb)))
  1085. return 1;
  1086. if (unlikely(len > skb->len))
  1087. return 0;
  1088. return __pskb_pull_tail(skb, len - skb_headlen(skb)) != NULL;
  1089. }
  1090. /**
  1091. * skb_headroom - bytes at buffer head
  1092. * @skb: buffer to check
  1093. *
  1094. * Return the number of bytes of free space at the head of an &sk_buff.
  1095. */
  1096. static inline unsigned int skb_headroom(const struct sk_buff *skb)
  1097. {
  1098. return skb->data - skb->head;
  1099. }
  1100. /**
  1101. * skb_tailroom - bytes at buffer end
  1102. * @skb: buffer to check
  1103. *
  1104. * Return the number of bytes of free space at the tail of an sk_buff
  1105. */
  1106. static inline int skb_tailroom(const struct sk_buff *skb)
  1107. {
  1108. return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
  1109. }
  1110. /**
  1111. * skb_reserve - adjust headroom
  1112. * @skb: buffer to alter
  1113. * @len: bytes to move
  1114. *
  1115. * Increase the headroom of an empty &sk_buff by reducing the tail
  1116. * room. This is only allowed for an empty buffer.
  1117. */
  1118. static inline void skb_reserve(struct sk_buff *skb, int len)
  1119. {
  1120. skb->data += len;
  1121. skb->tail += len;
  1122. }
  1123. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  1124. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  1125. {
  1126. return skb->head + skb->transport_header;
  1127. }
  1128. static inline void skb_reset_transport_header(struct sk_buff *skb)
  1129. {
  1130. skb->transport_header = skb->data - skb->head;
  1131. }
  1132. static inline void skb_set_transport_header(struct sk_buff *skb,
  1133. const int offset)
  1134. {
  1135. skb_reset_transport_header(skb);
  1136. skb->transport_header += offset;
  1137. }
  1138. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  1139. {
  1140. return skb->head + skb->network_header;
  1141. }
  1142. static inline void skb_reset_network_header(struct sk_buff *skb)
  1143. {
  1144. skb->network_header = skb->data - skb->head;
  1145. }
  1146. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  1147. {
  1148. skb_reset_network_header(skb);
  1149. skb->network_header += offset;
  1150. }
  1151. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  1152. {
  1153. return skb->head + skb->mac_header;
  1154. }
  1155. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  1156. {
  1157. return skb->mac_header != ~0U;
  1158. }
  1159. static inline void skb_reset_mac_header(struct sk_buff *skb)
  1160. {
  1161. skb->mac_header = skb->data - skb->head;
  1162. }
  1163. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  1164. {
  1165. skb_reset_mac_header(skb);
  1166. skb->mac_header += offset;
  1167. }
  1168. #else /* NET_SKBUFF_DATA_USES_OFFSET */
  1169. static inline unsigned char *skb_transport_header(const struct sk_buff *skb)
  1170. {
  1171. return skb->transport_header;
  1172. }
  1173. static inline void skb_reset_transport_header(struct sk_buff *skb)
  1174. {
  1175. skb->transport_header = skb->data;
  1176. }
  1177. static inline void skb_set_transport_header(struct sk_buff *skb,
  1178. const int offset)
  1179. {
  1180. skb->transport_header = skb->data + offset;
  1181. }
  1182. static inline unsigned char *skb_network_header(const struct sk_buff *skb)
  1183. {
  1184. return skb->network_header;
  1185. }
  1186. static inline void skb_reset_network_header(struct sk_buff *skb)
  1187. {
  1188. skb->network_header = skb->data;
  1189. }
  1190. static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
  1191. {
  1192. skb->network_header = skb->data + offset;
  1193. }
  1194. static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
  1195. {
  1196. return skb->mac_header;
  1197. }
  1198. static inline int skb_mac_header_was_set(const struct sk_buff *skb)
  1199. {
  1200. return skb->mac_header != NULL;
  1201. }
  1202. static inline void skb_reset_mac_header(struct sk_buff *skb)
  1203. {
  1204. skb->mac_header = skb->data;
  1205. }
  1206. static inline void skb_set_mac_header(struct sk_buff *skb, const int offset)
  1207. {
  1208. skb->mac_header = skb->data + offset;
  1209. }
  1210. #endif /* NET_SKBUFF_DATA_USES_OFFSET */
  1211. static inline int skb_checksum_start_offset(const struct sk_buff *skb)
  1212. {
  1213. return skb->csum_start - skb_headroom(skb);
  1214. }
  1215. static inline int skb_transport_offset(const struct sk_buff *skb)
  1216. {
  1217. return skb_transport_header(skb) - skb->data;
  1218. }
  1219. static inline u32 skb_network_header_len(const struct sk_buff *skb)
  1220. {
  1221. return skb->transport_header - skb->network_header;
  1222. }
  1223. static inline int skb_network_offset(const struct sk_buff *skb)
  1224. {
  1225. return skb_network_header(skb) - skb->data;
  1226. }
  1227. static inline int pskb_network_may_pull(struct sk_buff *skb, unsigned int len)
  1228. {
  1229. return pskb_may_pull(skb, skb_network_offset(skb) + len);
  1230. }
  1231. /*
  1232. * CPUs often take a performance hit when accessing unaligned memory
  1233. * locations. The actual performance hit varies, it can be small if the
  1234. * hardware handles it or large if we have to take an exception and fix it
  1235. * in software.
  1236. *
  1237. * Since an ethernet header is 14 bytes network drivers often end up with
  1238. * the IP header at an unaligned offset. The IP header can be aligned by
  1239. * shifting the start of the packet by 2 bytes. Drivers should do this
  1240. * with:
  1241. *
  1242. * skb_reserve(skb, NET_IP_ALIGN);
  1243. *
  1244. * The downside to this alignment of the IP header is that the DMA is now
  1245. * unaligned. On some architectures the cost of an unaligned DMA is high
  1246. * and this cost outweighs the gains made by aligning the IP header.
  1247. *
  1248. * Since this trade off varies between architectures, we allow NET_IP_ALIGN
  1249. * to be overridden.
  1250. */
  1251. #ifndef NET_IP_ALIGN
  1252. #define NET_IP_ALIGN 2
  1253. #endif
  1254. /*
  1255. * The networking layer reserves some headroom in skb data (via
  1256. * dev_alloc_skb). This is used to avoid having to reallocate skb data when
  1257. * the header has to grow. In the default case, if the header has to grow
  1258. * 32 bytes or less we avoid the reallocation.
  1259. *
  1260. * Unfortunately this headroom changes the DMA alignment of the resulting
  1261. * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
  1262. * on some architectures. An architecture can override this value,
  1263. * perhaps setting it to a cacheline in size (since that will maintain
  1264. * cacheline alignment of the DMA). It must be a power of 2.
  1265. *
  1266. * Various parts of the networking layer expect at least 32 bytes of
  1267. * headroom, you should not reduce this.
  1268. *
  1269. * Using max(32, L1_CACHE_BYTES) makes sense (especially with RPS)
  1270. * to reduce average number of cache lines per packet.
  1271. * get_rps_cpus() for example only access one 64 bytes aligned block :
  1272. * NET_IP_ALIGN(2) + ethernet_header(14) + IP_header(20/40) + ports(8)
  1273. */
  1274. #ifndef NET_SKB_PAD
  1275. #define NET_SKB_PAD max(32, L1_CACHE_BYTES)
  1276. #endif
  1277. extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
  1278. static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
  1279. {
  1280. if (unlikely(skb->data_len)) {
  1281. WARN_ON(1);
  1282. return;
  1283. }
  1284. skb->len = len;
  1285. skb_set_tail_pointer(skb, len);
  1286. }
  1287. extern void skb_trim(struct sk_buff *skb, unsigned int len);
  1288. static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
  1289. {
  1290. if (skb->data_len)
  1291. return ___pskb_trim(skb, len);
  1292. __skb_trim(skb, len);
  1293. return 0;
  1294. }
  1295. static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
  1296. {
  1297. return (len < skb->len) ? __pskb_trim(skb, len) : 0;
  1298. }
  1299. /**
  1300. * pskb_trim_unique - remove end from a paged unique (not cloned) buffer
  1301. * @skb: buffer to alter
  1302. * @len: new length
  1303. *
  1304. * This is identical to pskb_trim except that the caller knows that
  1305. * the skb is not cloned so we should never get an error due to out-
  1306. * of-memory.
  1307. */
  1308. static inline void pskb_trim_unique(struct sk_buff *skb, unsigned int len)
  1309. {
  1310. int err = pskb_trim(skb, len);
  1311. BUG_ON(err);
  1312. }
  1313. /**
  1314. * skb_orphan - orphan a buffer
  1315. * @skb: buffer to orphan
  1316. *
  1317. * If a buffer currently has an owner then we call the owner's
  1318. * destructor function and make the @skb unowned. The buffer continues
  1319. * to exist but is no longer charged to its former owner.
  1320. */
  1321. static inline void skb_orphan(struct sk_buff *skb)
  1322. {
  1323. if (skb->destructor)
  1324. skb->destructor(skb);
  1325. skb->destructor = NULL;
  1326. skb->sk = NULL;
  1327. }
  1328. /**
  1329. * __skb_queue_purge - empty a list
  1330. * @list: list to empty
  1331. *
  1332. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  1333. * the list and one reference dropped. This function does not take the
  1334. * list lock and the caller must hold the relevant locks to use it.
  1335. */
  1336. extern void skb_queue_purge(struct sk_buff_head *list);
  1337. static inline void __skb_queue_purge(struct sk_buff_head *list)
  1338. {
  1339. struct sk_buff *skb;
  1340. while ((skb = __skb_dequeue(list)) != NULL)
  1341. kfree_skb(skb);
  1342. }
  1343. /**
  1344. * __dev_alloc_skb - allocate an skbuff for receiving
  1345. * @length: length to allocate
  1346. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  1347. *
  1348. * Allocate a new &sk_buff and assign it a usage count of one. The
  1349. * buffer has unspecified headroom built in. Users should allocate
  1350. * the headroom they think they need without accounting for the
  1351. * built in space. The built in space is used for optimisations.
  1352. *
  1353. * %NULL is returned if there is no free memory.
  1354. */
  1355. static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
  1356. gfp_t gfp_mask)
  1357. {
  1358. struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
  1359. if (likely(skb))
  1360. skb_reserve(skb, NET_SKB_PAD);
  1361. return skb;
  1362. }
  1363. extern struct sk_buff *dev_alloc_skb(unsigned int length);
  1364. extern struct sk_buff *__netdev_alloc_skb(struct net_device *dev,
  1365. unsigned int length, gfp_t gfp_mask);
  1366. /**
  1367. * netdev_alloc_skb - allocate an skbuff for rx on a specific device
  1368. * @dev: network device to receive on
  1369. * @length: length to allocate
  1370. *
  1371. * Allocate a new &sk_buff and assign it a usage count of one. The
  1372. * buffer has unspecified headroom built in. Users should allocate
  1373. * the headroom they think they need without accounting for the
  1374. * built in space. The built in space is used for optimisations.
  1375. *
  1376. * %NULL is returned if there is no free memory. Although this function
  1377. * allocates memory it can be called from an interrupt.
  1378. */
  1379. static inline struct sk_buff *netdev_alloc_skb(struct net_device *dev,
  1380. unsigned int length)
  1381. {
  1382. return __netdev_alloc_skb(dev, length, GFP_ATOMIC);
  1383. }
  1384. static inline struct sk_buff *netdev_alloc_skb_ip_align(struct net_device *dev,
  1385. unsigned int length)
  1386. {
  1387. struct sk_buff *skb = netdev_alloc_skb(dev, length + NET_IP_ALIGN);
  1388. if (NET_IP_ALIGN && skb)
  1389. skb_reserve(skb, NET_IP_ALIGN);
  1390. return skb;
  1391. }
  1392. /**
  1393. * __netdev_alloc_page - allocate a page for ps-rx on a specific device
  1394. * @dev: network device to receive on
  1395. * @gfp_mask: alloc_pages_node mask
  1396. *
  1397. * Allocate a new page. dev currently unused.
  1398. *
  1399. * %NULL is returned if there is no free memory.
  1400. */
  1401. static inline struct page *__netdev_alloc_page(struct net_device *dev, gfp_t gfp_mask)
  1402. {
  1403. return alloc_pages_node(NUMA_NO_NODE, gfp_mask, 0);
  1404. }
  1405. /**
  1406. * netdev_alloc_page - allocate a page for ps-rx on a specific device
  1407. * @dev: network device to receive on
  1408. *
  1409. * Allocate a new page. dev currently unused.
  1410. *
  1411. * %NULL is returned if there is no free memory.
  1412. */
  1413. static inline struct page *netdev_alloc_page(struct net_device *dev)
  1414. {
  1415. return __netdev_alloc_page(dev, GFP_ATOMIC);
  1416. }
  1417. static inline void netdev_free_page(struct net_device *dev, struct page *page)
  1418. {
  1419. __free_page(page);
  1420. }
  1421. /**
  1422. * skb_clone_writable - is the header of a clone writable
  1423. * @skb: buffer to check
  1424. * @len: length up to which to write
  1425. *
  1426. * Returns true if modifying the header part of the cloned buffer
  1427. * does not requires the data to be copied.
  1428. */
  1429. static inline int skb_clone_writable(struct sk_buff *skb, unsigned int len)
  1430. {
  1431. return !skb_header_cloned(skb) &&
  1432. skb_headroom(skb) + len <= skb->hdr_len;
  1433. }
  1434. static inline int __skb_cow(struct sk_buff *skb, unsigned int headroom,
  1435. int cloned)
  1436. {
  1437. int delta = 0;
  1438. if (headroom < NET_SKB_PAD)
  1439. headroom = NET_SKB_PAD;
  1440. if (headroom > skb_headroom(skb))
  1441. delta = headroom - skb_headroom(skb);
  1442. if (delta || cloned)
  1443. return pskb_expand_head(skb, ALIGN(delta, NET_SKB_PAD), 0,
  1444. GFP_ATOMIC);
  1445. return 0;
  1446. }
  1447. /**
  1448. * skb_cow - copy header of skb when it is required
  1449. * @skb: buffer to cow
  1450. * @headroom: needed headroom
  1451. *
  1452. * If the skb passed lacks sufficient headroom or its data part
  1453. * is shared, data is reallocated. If reallocation fails, an error
  1454. * is returned and original skb is not changed.
  1455. *
  1456. * The result is skb with writable area skb->head...skb->tail
  1457. * and at least @headroom of space at head.
  1458. */
  1459. static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
  1460. {
  1461. return __skb_cow(skb, headroom, skb_cloned(skb));
  1462. }
  1463. /**
  1464. * skb_cow_head - skb_cow but only making the head writable
  1465. * @skb: buffer to cow
  1466. * @headroom: needed headroom
  1467. *
  1468. * This function is identical to skb_cow except that we replace the
  1469. * skb_cloned check by skb_header_cloned. It should be used when
  1470. * you only need to push on some header and do not need to modify
  1471. * the data.
  1472. */
  1473. static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
  1474. {
  1475. return __skb_cow(skb, headroom, skb_header_cloned(skb));
  1476. }
  1477. /**
  1478. * skb_padto - pad an skbuff up to a minimal size
  1479. * @skb: buffer to pad
  1480. * @len: minimal length
  1481. *
  1482. * Pads up a buffer to ensure the trailing bytes exist and are
  1483. * blanked. If the buffer already contains sufficient data it
  1484. * is untouched. Otherwise it is extended. Returns zero on
  1485. * success. The skb is freed on error.
  1486. */
  1487. static inline int skb_padto(struct sk_buff *skb, unsigned int len)
  1488. {
  1489. unsigned int size = skb->len;
  1490. if (likely(size >= len))
  1491. return 0;
  1492. return skb_pad(skb, len - size);
  1493. }
  1494. static inline int skb_add_data(struct sk_buff *skb,
  1495. char __user *from, int copy)
  1496. {
  1497. const int off = skb->len;
  1498. if (skb->ip_summed == CHECKSUM_NONE) {
  1499. int err = 0;
  1500. __wsum csum = csum_and_copy_from_user(from, skb_put(skb, copy),
  1501. copy, 0, &err);
  1502. if (!err) {
  1503. skb->csum = csum_block_add(skb->csum, csum, off);
  1504. return 0;
  1505. }
  1506. } else if (!copy_from_user(skb_put(skb, copy), from, copy))
  1507. return 0;
  1508. __skb_trim(skb, off);
  1509. return -EFAULT;
  1510. }
  1511. static inline int skb_can_coalesce(struct sk_buff *skb, int i,
  1512. struct page *page, int off)
  1513. {
  1514. if (i) {
  1515. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1516. return page == frag->page &&
  1517. off == frag->page_offset + frag->size;
  1518. }
  1519. return 0;
  1520. }
  1521. static inline int __skb_linearize(struct sk_buff *skb)
  1522. {
  1523. return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
  1524. }
  1525. /**
  1526. * skb_linearize - convert paged skb to linear one
  1527. * @skb: buffer to linarize
  1528. *
  1529. * If there is no free memory -ENOMEM is returned, otherwise zero
  1530. * is returned and the old skb data released.
  1531. */
  1532. static inline int skb_linearize(struct sk_buff *skb)
  1533. {
  1534. return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
  1535. }
  1536. /**
  1537. * skb_linearize_cow - make sure skb is linear and writable
  1538. * @skb: buffer to process
  1539. *
  1540. * If there is no free memory -ENOMEM is returned, otherwise zero
  1541. * is returned and the old skb data released.
  1542. */
  1543. static inline int skb_linearize_cow(struct sk_buff *skb)
  1544. {
  1545. return skb_is_nonlinear(skb) || skb_cloned(skb) ?
  1546. __skb_linearize(skb) : 0;
  1547. }
  1548. /**
  1549. * skb_postpull_rcsum - update checksum for received skb after pull
  1550. * @skb: buffer to update
  1551. * @start: start of data before pull
  1552. * @len: length of data pulled
  1553. *
  1554. * After doing a pull on a received packet, you need to call this to
  1555. * update the CHECKSUM_COMPLETE checksum, or set ip_summed to
  1556. * CHECKSUM_NONE so that it can be recomputed from scratch.
  1557. */
  1558. static inline void skb_postpull_rcsum(struct sk_buff *skb,
  1559. const void *start, unsigned int len)
  1560. {
  1561. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1562. skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
  1563. }
  1564. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
  1565. /**
  1566. * pskb_trim_rcsum - trim received skb and update checksum
  1567. * @skb: buffer to trim
  1568. * @len: new length
  1569. *
  1570. * This is exactly the same as pskb_trim except that it ensures the
  1571. * checksum of received packets are still valid after the operation.
  1572. */
  1573. static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
  1574. {
  1575. if (likely(len >= skb->len))
  1576. return 0;
  1577. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1578. skb->ip_summed = CHECKSUM_NONE;
  1579. return __pskb_trim(skb, len);
  1580. }
  1581. #define skb_queue_walk(queue, skb) \
  1582. for (skb = (queue)->next; \
  1583. skb != (struct sk_buff *)(queue); \
  1584. skb = skb->next)
  1585. #define skb_queue_walk_safe(queue, skb, tmp) \
  1586. for (skb = (queue)->next, tmp = skb->next; \
  1587. skb != (struct sk_buff *)(queue); \
  1588. skb = tmp, tmp = skb->next)
  1589. #define skb_queue_walk_from(queue, skb) \
  1590. for (; skb != (struct sk_buff *)(queue); \
  1591. skb = skb->next)
  1592. #define skb_queue_walk_from_safe(queue, skb, tmp) \
  1593. for (tmp = skb->next; \
  1594. skb != (struct sk_buff *)(queue); \
  1595. skb = tmp, tmp = skb->next)
  1596. #define skb_queue_reverse_walk(queue, skb) \
  1597. for (skb = (queue)->prev; \
  1598. skb != (struct sk_buff *)(queue); \
  1599. skb = skb->prev)
  1600. #define skb_queue_reverse_walk_safe(queue, skb, tmp) \
  1601. for (skb = (queue)->prev, tmp = skb->prev; \
  1602. skb != (struct sk_buff *)(queue); \
  1603. skb = tmp, tmp = skb->prev)
  1604. #define skb_queue_reverse_walk_from_safe(queue, skb, tmp) \
  1605. for (tmp = skb->prev; \
  1606. skb != (struct sk_buff *)(queue); \
  1607. skb = tmp, tmp = skb->prev)
  1608. static inline bool skb_has_frag_list(const struct sk_buff *skb)
  1609. {
  1610. return skb_shinfo(skb)->frag_list != NULL;
  1611. }
  1612. static inline void skb_frag_list_init(struct sk_buff *skb)
  1613. {
  1614. skb_shinfo(skb)->frag_list = NULL;
  1615. }
  1616. static inline void skb_frag_add_head(struct sk_buff *skb, struct sk_buff *frag)
  1617. {
  1618. frag->next = skb_shinfo(skb)->frag_list;
  1619. skb_shinfo(skb)->frag_list = frag;
  1620. }
  1621. #define skb_walk_frags(skb, iter) \
  1622. for (iter = skb_shinfo(skb)->frag_list; iter; iter = iter->next)
  1623. extern struct sk_buff *__skb_recv_datagram(struct sock *sk, unsigned flags,
  1624. int *peeked, int *err);
  1625. extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
  1626. int noblock, int *err);
  1627. extern unsigned int datagram_poll(struct file *file, struct socket *sock,
  1628. struct poll_table_struct *wait);
  1629. extern int skb_copy_datagram_iovec(const struct sk_buff *from,
  1630. int offset, struct iovec *to,
  1631. int size);
  1632. extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  1633. int hlen,
  1634. struct iovec *iov);
  1635. extern int skb_copy_datagram_from_iovec(struct sk_buff *skb,
  1636. int offset,
  1637. const struct iovec *from,
  1638. int from_offset,
  1639. int len);
  1640. extern int skb_copy_datagram_const_iovec(const struct sk_buff *from,
  1641. int offset,
  1642. const struct iovec *to,
  1643. int to_offset,
  1644. int size);
  1645. extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
  1646. extern void skb_free_datagram_locked(struct sock *sk,
  1647. struct sk_buff *skb);
  1648. extern int skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
  1649. unsigned int flags);
  1650. extern __wsum skb_checksum(const struct sk_buff *skb, int offset,
  1651. int len, __wsum csum);
  1652. extern int skb_copy_bits(const struct sk_buff *skb, int offset,
  1653. void *to, int len);
  1654. extern int skb_store_bits(struct sk_buff *skb, int offset,
  1655. const void *from, int len);
  1656. extern __wsum skb_copy_and_csum_bits(const struct sk_buff *skb,
  1657. int offset, u8 *to, int len,
  1658. __wsum csum);
  1659. extern int skb_splice_bits(struct sk_buff *skb,
  1660. unsigned int offset,
  1661. struct pipe_inode_info *pipe,
  1662. unsigned int len,
  1663. unsigned int flags);
  1664. extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
  1665. extern void skb_split(struct sk_buff *skb,
  1666. struct sk_buff *skb1, const u32 len);
  1667. extern int skb_shift(struct sk_buff *tgt, struct sk_buff *skb,
  1668. int shiftlen);
  1669. extern struct sk_buff *skb_segment(struct sk_buff *skb, u32 features);
  1670. static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
  1671. int len, void *buffer)
  1672. {
  1673. int hlen = skb_headlen(skb);
  1674. if (hlen - offset >= len)
  1675. return skb->data + offset;
  1676. if (skb_copy_bits(skb, offset, buffer, len) < 0)
  1677. return NULL;
  1678. return buffer;
  1679. }
  1680. static inline void skb_copy_from_linear_data(const struct sk_buff *skb,
  1681. void *to,
  1682. const unsigned int len)
  1683. {
  1684. memcpy(to, skb->data, len);
  1685. }
  1686. static inline void skb_copy_from_linear_data_offset(const struct sk_buff *skb,
  1687. const int offset, void *to,
  1688. const unsigned int len)
  1689. {
  1690. memcpy(to, skb->data + offset, len);
  1691. }
  1692. static inline void skb_copy_to_linear_data(struct sk_buff *skb,
  1693. const void *from,
  1694. const unsigned int len)
  1695. {
  1696. memcpy(skb->data, from, len);
  1697. }
  1698. static inline void skb_copy_to_linear_data_offset(struct sk_buff *skb,
  1699. const int offset,
  1700. const void *from,
  1701. const unsigned int len)
  1702. {
  1703. memcpy(skb->data + offset, from, len);
  1704. }
  1705. extern void skb_init(void);
  1706. static inline ktime_t skb_get_ktime(const struct sk_buff *skb)
  1707. {
  1708. return skb->tstamp;
  1709. }
  1710. /**
  1711. * skb_get_timestamp - get timestamp from a skb
  1712. * @skb: skb to get stamp from
  1713. * @stamp: pointer to struct timeval to store stamp in
  1714. *
  1715. * Timestamps are stored in the skb as offsets to a base timestamp.
  1716. * This function converts the offset back to a struct timeval and stores
  1717. * it in stamp.
  1718. */
  1719. static inline void skb_get_timestamp(const struct sk_buff *skb,
  1720. struct timeval *stamp)
  1721. {
  1722. *stamp = ktime_to_timeval(skb->tstamp);
  1723. }
  1724. static inline void skb_get_timestampns(const struct sk_buff *skb,
  1725. struct timespec *stamp)
  1726. {
  1727. *stamp = ktime_to_timespec(skb->tstamp);
  1728. }
  1729. static inline void __net_timestamp(struct sk_buff *skb)
  1730. {
  1731. skb->tstamp = ktime_get_real();
  1732. }
  1733. static inline ktime_t net_timedelta(ktime_t t)
  1734. {
  1735. return ktime_sub(ktime_get_real(), t);
  1736. }
  1737. static inline ktime_t net_invalid_timestamp(void)
  1738. {
  1739. return ktime_set(0, 0);
  1740. }
  1741. extern void skb_timestamping_init(void);
  1742. #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING
  1743. extern void skb_clone_tx_timestamp(struct sk_buff *skb);
  1744. extern bool skb_defer_rx_timestamp(struct sk_buff *skb);
  1745. #else /* CONFIG_NETWORK_PHY_TIMESTAMPING */
  1746. static inline void skb_clone_tx_timestamp(struct sk_buff *skb)
  1747. {
  1748. }
  1749. static inline bool skb_defer_rx_timestamp(struct sk_buff *skb)
  1750. {
  1751. return false;
  1752. }
  1753. #endif /* !CONFIG_NETWORK_PHY_TIMESTAMPING */
  1754. /**
  1755. * skb_complete_tx_timestamp() - deliver cloned skb with tx timestamps
  1756. *
  1757. * @skb: clone of the the original outgoing packet
  1758. * @hwtstamps: hardware time stamps
  1759. *
  1760. */
  1761. void skb_complete_tx_timestamp(struct sk_buff *skb,
  1762. struct skb_shared_hwtstamps *hwtstamps);
  1763. /**
  1764. * skb_tstamp_tx - queue clone of skb with send time stamps
  1765. * @orig_skb: the original outgoing packet
  1766. * @hwtstamps: hardware time stamps, may be NULL if not available
  1767. *
  1768. * If the skb has a socket associated, then this function clones the
  1769. * skb (thus sharing the actual data and optional structures), stores
  1770. * the optional hardware time stamping information (if non NULL) or
  1771. * generates a software time stamp (otherwise), then queues the clone
  1772. * to the error queue of the socket. Errors are silently ignored.
  1773. */
  1774. extern void skb_tstamp_tx(struct sk_buff *orig_skb,
  1775. struct skb_shared_hwtstamps *hwtstamps);
  1776. static inline void sw_tx_timestamp(struct sk_buff *skb)
  1777. {
  1778. if (skb_shinfo(skb)->tx_flags & SKBTX_SW_TSTAMP &&
  1779. !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
  1780. skb_tstamp_tx(skb, NULL);
  1781. }
  1782. /**
  1783. * skb_tx_timestamp() - Driver hook for transmit timestamping
  1784. *
  1785. * Ethernet MAC Drivers should call this function in their hard_xmit()
  1786. * function as soon as possible after giving the sk_buff to the MAC
  1787. * hardware, but before freeing the sk_buff.
  1788. *
  1789. * @skb: A socket buffer.
  1790. */
  1791. static inline void skb_tx_timestamp(struct sk_buff *skb)
  1792. {
  1793. skb_clone_tx_timestamp(skb);
  1794. sw_tx_timestamp(skb);
  1795. }
  1796. extern __sum16 __skb_checksum_complete_head(struct sk_buff *skb, int len);
  1797. extern __sum16 __skb_checksum_complete(struct sk_buff *skb);
  1798. static inline int skb_csum_unnecessary(const struct sk_buff *skb)
  1799. {
  1800. return skb->ip_summed & CHECKSUM_UNNECESSARY;
  1801. }
  1802. /**
  1803. * skb_checksum_complete - Calculate checksum of an entire packet
  1804. * @skb: packet to process
  1805. *
  1806. * This function calculates the checksum over the entire packet plus
  1807. * the value of skb->csum. The latter can be used to supply the
  1808. * checksum of a pseudo header as used by TCP/UDP. It returns the
  1809. * checksum.
  1810. *
  1811. * For protocols that contain complete checksums such as ICMP/TCP/UDP,
  1812. * this function can be used to verify that checksum on received
  1813. * packets. In that case the function should return zero if the
  1814. * checksum is correct. In particular, this function will return zero
  1815. * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
  1816. * hardware has already verified the correctness of the checksum.
  1817. */
  1818. static inline __sum16 skb_checksum_complete(struct sk_buff *skb)
  1819. {
  1820. return skb_csum_unnecessary(skb) ?
  1821. 0 : __skb_checksum_complete(skb);
  1822. }
  1823. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1824. extern void nf_conntrack_destroy(struct nf_conntrack *nfct);
  1825. static inline void nf_conntrack_put(struct nf_conntrack *nfct)
  1826. {
  1827. if (nfct && atomic_dec_and_test(&nfct->use))
  1828. nf_conntrack_destroy(nfct);
  1829. }
  1830. static inline void nf_conntrack_get(struct nf_conntrack *nfct)
  1831. {
  1832. if (nfct)
  1833. atomic_inc(&nfct->use);
  1834. }
  1835. #endif
  1836. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  1837. static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
  1838. {
  1839. if (skb)
  1840. atomic_inc(&skb->users);
  1841. }
  1842. static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
  1843. {
  1844. if (skb)
  1845. kfree_skb(skb);
  1846. }
  1847. #endif
  1848. #ifdef CONFIG_BRIDGE_NETFILTER
  1849. static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
  1850. {
  1851. if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
  1852. kfree(nf_bridge);
  1853. }
  1854. static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
  1855. {
  1856. if (nf_bridge)
  1857. atomic_inc(&nf_bridge->use);
  1858. }
  1859. #endif /* CONFIG_BRIDGE_NETFILTER */
  1860. static inline void nf_reset(struct sk_buff *skb)
  1861. {
  1862. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1863. nf_conntrack_put(skb->nfct);
  1864. skb->nfct = NULL;
  1865. #endif
  1866. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  1867. nf_conntrack_put_reasm(skb->nfct_reasm);
  1868. skb->nfct_reasm = NULL;
  1869. #endif
  1870. #ifdef CONFIG_BRIDGE_NETFILTER
  1871. nf_bridge_put(skb->nf_bridge);
  1872. skb->nf_bridge = NULL;
  1873. #endif
  1874. }
  1875. /* Note: This doesn't put any conntrack and bridge info in dst. */
  1876. static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  1877. {
  1878. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1879. dst->nfct = src->nfct;
  1880. nf_conntrack_get(src->nfct);
  1881. dst->nfctinfo = src->nfctinfo;
  1882. #endif
  1883. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  1884. dst->nfct_reasm = src->nfct_reasm;
  1885. nf_conntrack_get_reasm(src->nfct_reasm);
  1886. #endif
  1887. #ifdef CONFIG_BRIDGE_NETFILTER
  1888. dst->nf_bridge = src->nf_bridge;
  1889. nf_bridge_get(src->nf_bridge);
  1890. #endif
  1891. }
  1892. static inline void nf_copy(struct sk_buff *dst, const struct sk_buff *src)
  1893. {
  1894. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1895. nf_conntrack_put(dst->nfct);
  1896. #endif
  1897. #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
  1898. nf_conntrack_put_reasm(dst->nfct_reasm);
  1899. #endif
  1900. #ifdef CONFIG_BRIDGE_NETFILTER
  1901. nf_bridge_put(dst->nf_bridge);
  1902. #endif
  1903. __nf_copy(dst, src);
  1904. }
  1905. #ifdef CONFIG_NETWORK_SECMARK
  1906. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  1907. {
  1908. to->secmark = from->secmark;
  1909. }
  1910. static inline void skb_init_secmark(struct sk_buff *skb)
  1911. {
  1912. skb->secmark = 0;
  1913. }
  1914. #else
  1915. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  1916. { }
  1917. static inline void skb_init_secmark(struct sk_buff *skb)
  1918. { }
  1919. #endif
  1920. static inline void skb_set_queue_mapping(struct sk_buff *skb, u16 queue_mapping)
  1921. {
  1922. skb->queue_mapping = queue_mapping;
  1923. }
  1924. static inline u16 skb_get_queue_mapping(const struct sk_buff *skb)
  1925. {
  1926. return skb->queue_mapping;
  1927. }
  1928. static inline void skb_copy_queue_mapping(struct sk_buff *to, const struct sk_buff *from)
  1929. {
  1930. to->queue_mapping = from->queue_mapping;
  1931. }
  1932. static inline void skb_record_rx_queue(struct sk_buff *skb, u16 rx_queue)
  1933. {
  1934. skb->queue_mapping = rx_queue + 1;
  1935. }
  1936. static inline u16 skb_get_rx_queue(const struct sk_buff *skb)
  1937. {
  1938. return skb->queue_mapping - 1;
  1939. }
  1940. static inline bool skb_rx_queue_recorded(const struct sk_buff *skb)
  1941. {
  1942. return skb->queue_mapping != 0;
  1943. }
  1944. extern u16 __skb_tx_hash(const struct net_device *dev,
  1945. const struct sk_buff *skb,
  1946. unsigned int num_tx_queues);
  1947. #ifdef CONFIG_XFRM
  1948. static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
  1949. {
  1950. return skb->sp;
  1951. }
  1952. #else
  1953. static inline struct sec_path *skb_sec_path(struct sk_buff *skb)
  1954. {
  1955. return NULL;
  1956. }
  1957. #endif
  1958. static inline int skb_is_gso(const struct sk_buff *skb)
  1959. {
  1960. return skb_shinfo(skb)->gso_size;
  1961. }
  1962. static inline int skb_is_gso_v6(const struct sk_buff *skb)
  1963. {
  1964. return skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6;
  1965. }
  1966. extern void __skb_warn_lro_forwarding(const struct sk_buff *skb);
  1967. static inline bool skb_warn_if_lro(const struct sk_buff *skb)
  1968. {
  1969. /* LRO sets gso_size but not gso_type, whereas if GSO is really
  1970. * wanted then gso_type will be set. */
  1971. struct skb_shared_info *shinfo = skb_shinfo(skb);
  1972. if (skb_is_nonlinear(skb) && shinfo->gso_size != 0 &&
  1973. unlikely(shinfo->gso_type == 0)) {
  1974. __skb_warn_lro_forwarding(skb);
  1975. return true;
  1976. }
  1977. return false;
  1978. }
  1979. static inline void skb_forward_csum(struct sk_buff *skb)
  1980. {
  1981. /* Unfortunately we don't support this one. Any brave souls? */
  1982. if (skb->ip_summed == CHECKSUM_COMPLETE)
  1983. skb->ip_summed = CHECKSUM_NONE;
  1984. }
  1985. /**
  1986. * skb_checksum_none_assert - make sure skb ip_summed is CHECKSUM_NONE
  1987. * @skb: skb to check
  1988. *
  1989. * fresh skbs have their ip_summed set to CHECKSUM_NONE.
  1990. * Instead of forcing ip_summed to CHECKSUM_NONE, we can
  1991. * use this helper, to document places where we make this assertion.
  1992. */
  1993. static inline void skb_checksum_none_assert(struct sk_buff *skb)
  1994. {
  1995. #ifdef DEBUG
  1996. BUG_ON(skb->ip_summed != CHECKSUM_NONE);
  1997. #endif
  1998. }
  1999. bool skb_partial_csum_set(struct sk_buff *skb, u16 start, u16 off);
  2000. #endif /* __KERNEL__ */
  2001. #endif /* _LINUX_SKBUFF_H */