skbuff.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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/compiler.h>
  17. #include <linux/time.h>
  18. #include <linux/cache.h>
  19. #include <asm/atomic.h>
  20. #include <asm/types.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/mm.h>
  23. #include <linux/highmem.h>
  24. #include <linux/poll.h>
  25. #include <linux/net.h>
  26. #include <linux/textsearch.h>
  27. #include <net/checksum.h>
  28. #include <linux/dmaengine.h>
  29. #define HAVE_ALLOC_SKB /* For the drivers to know */
  30. #define HAVE_ALIGNABLE_SKB /* Ditto 8) */
  31. #define CHECKSUM_NONE 0
  32. #define CHECKSUM_HW 1
  33. #define CHECKSUM_UNNECESSARY 2
  34. #define SKB_DATA_ALIGN(X) (((X) + (SMP_CACHE_BYTES - 1)) & \
  35. ~(SMP_CACHE_BYTES - 1))
  36. #define SKB_MAX_ORDER(X, ORDER) (((PAGE_SIZE << (ORDER)) - (X) - \
  37. sizeof(struct skb_shared_info)) & \
  38. ~(SMP_CACHE_BYTES - 1))
  39. #define SKB_MAX_HEAD(X) (SKB_MAX_ORDER((X), 0))
  40. #define SKB_MAX_ALLOC (SKB_MAX_ORDER(0, 2))
  41. /* A. Checksumming of received packets by device.
  42. *
  43. * NONE: device failed to checksum this packet.
  44. * skb->csum is undefined.
  45. *
  46. * UNNECESSARY: device parsed packet and wouldbe verified checksum.
  47. * skb->csum is undefined.
  48. * It is bad option, but, unfortunately, many of vendors do this.
  49. * Apparently with secret goal to sell you new device, when you
  50. * will add new protocol to your host. F.e. IPv6. 8)
  51. *
  52. * HW: the most generic way. Device supplied checksum of _all_
  53. * the packet as seen by netif_rx in skb->csum.
  54. * NOTE: Even if device supports only some protocols, but
  55. * is able to produce some skb->csum, it MUST use HW,
  56. * not UNNECESSARY.
  57. *
  58. * B. Checksumming on output.
  59. *
  60. * NONE: skb is checksummed by protocol or csum is not required.
  61. *
  62. * HW: device is required to csum packet as seen by hard_start_xmit
  63. * from skb->h.raw to the end and to record the checksum
  64. * at skb->h.raw+skb->csum.
  65. *
  66. * Device must show its capabilities in dev->features, set
  67. * at device setup time.
  68. * NETIF_F_HW_CSUM - it is clever device, it is able to checksum
  69. * everything.
  70. * NETIF_F_NO_CSUM - loopback or reliable single hop media.
  71. * NETIF_F_IP_CSUM - device is dumb. It is able to csum only
  72. * TCP/UDP over IPv4. Sigh. Vendors like this
  73. * way by an unknown reason. Though, see comment above
  74. * about CHECKSUM_UNNECESSARY. 8)
  75. *
  76. * Any questions? No questions, good. --ANK
  77. */
  78. struct net_device;
  79. #ifdef CONFIG_NETFILTER
  80. struct nf_conntrack {
  81. atomic_t use;
  82. void (*destroy)(struct nf_conntrack *);
  83. };
  84. #ifdef CONFIG_BRIDGE_NETFILTER
  85. struct nf_bridge_info {
  86. atomic_t use;
  87. struct net_device *physindev;
  88. struct net_device *physoutdev;
  89. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  90. struct net_device *netoutdev;
  91. #endif
  92. unsigned int mask;
  93. unsigned long data[32 / sizeof(unsigned long)];
  94. };
  95. #endif
  96. #endif
  97. struct sk_buff_head {
  98. /* These two members must be first. */
  99. struct sk_buff *next;
  100. struct sk_buff *prev;
  101. __u32 qlen;
  102. spinlock_t lock;
  103. };
  104. struct sk_buff;
  105. /* To allow 64K frame to be packed as single skb without frag_list */
  106. #define MAX_SKB_FRAGS (65536/PAGE_SIZE + 2)
  107. typedef struct skb_frag_struct skb_frag_t;
  108. struct skb_frag_struct {
  109. struct page *page;
  110. __u16 page_offset;
  111. __u16 size;
  112. };
  113. /* This data is invariant across clones and lives at
  114. * the end of the header data, ie. at skb->end.
  115. */
  116. struct skb_shared_info {
  117. atomic_t dataref;
  118. unsigned short nr_frags;
  119. unsigned short gso_size;
  120. /* Warning: this field is not always filled in (UFO)! */
  121. unsigned short gso_segs;
  122. unsigned short gso_type;
  123. unsigned int ip6_frag_id;
  124. struct sk_buff *frag_list;
  125. skb_frag_t frags[MAX_SKB_FRAGS];
  126. };
  127. /* We divide dataref into two halves. The higher 16 bits hold references
  128. * to the payload part of skb->data. The lower 16 bits hold references to
  129. * the entire skb->data. It is up to the users of the skb to agree on
  130. * where the payload starts.
  131. *
  132. * All users must obey the rule that the skb->data reference count must be
  133. * greater than or equal to the payload reference count.
  134. *
  135. * Holding a reference to the payload part means that the user does not
  136. * care about modifications to the header part of skb->data.
  137. */
  138. #define SKB_DATAREF_SHIFT 16
  139. #define SKB_DATAREF_MASK ((1 << SKB_DATAREF_SHIFT) - 1)
  140. struct skb_timeval {
  141. u32 off_sec;
  142. u32 off_usec;
  143. };
  144. enum {
  145. SKB_FCLONE_UNAVAILABLE,
  146. SKB_FCLONE_ORIG,
  147. SKB_FCLONE_CLONE,
  148. };
  149. enum {
  150. SKB_GSO_TCPV4 = 1 << 0,
  151. SKB_GSO_UDPV4 = 1 << 1,
  152. /* This indicates the skb is from an untrusted source. */
  153. SKB_GSO_DODGY = 1 << 2,
  154. };
  155. /**
  156. * struct sk_buff - socket buffer
  157. * @next: Next buffer in list
  158. * @prev: Previous buffer in list
  159. * @sk: Socket we are owned by
  160. * @tstamp: Time we arrived
  161. * @dev: Device we arrived on/are leaving by
  162. * @input_dev: Device we arrived on
  163. * @h: Transport layer header
  164. * @nh: Network layer header
  165. * @mac: Link layer header
  166. * @dst: destination entry
  167. * @sp: the security path, used for xfrm
  168. * @cb: Control buffer. Free for use by every layer. Put private vars here
  169. * @len: Length of actual data
  170. * @data_len: Data length
  171. * @mac_len: Length of link layer header
  172. * @csum: Checksum
  173. * @local_df: allow local fragmentation
  174. * @cloned: Head may be cloned (check refcnt to be sure)
  175. * @nohdr: Payload reference only, must not modify header
  176. * @pkt_type: Packet class
  177. * @fclone: skbuff clone status
  178. * @ip_summed: Driver fed us an IP checksum
  179. * @priority: Packet queueing priority
  180. * @users: User count - see {datagram,tcp}.c
  181. * @protocol: Packet protocol from driver
  182. * @truesize: Buffer size
  183. * @head: Head of buffer
  184. * @data: Data head pointer
  185. * @tail: Tail pointer
  186. * @end: End pointer
  187. * @destructor: Destruct function
  188. * @nfmark: Can be used for communication between hooks
  189. * @nfct: Associated connection, if any
  190. * @ipvs_property: skbuff is owned by ipvs
  191. * @nfctinfo: Relationship of this skb to the connection
  192. * @nfct_reasm: netfilter conntrack re-assembly pointer
  193. * @nf_bridge: Saved data about a bridged frame - see br_netfilter.c
  194. * @tc_index: Traffic control index
  195. * @tc_verd: traffic control verdict
  196. * @dma_cookie: a cookie to one of several possible DMA operations
  197. * done by skb DMA functions
  198. * @secmark: security marking
  199. */
  200. struct sk_buff {
  201. /* These two members must be first. */
  202. struct sk_buff *next;
  203. struct sk_buff *prev;
  204. struct sock *sk;
  205. struct skb_timeval tstamp;
  206. struct net_device *dev;
  207. struct net_device *input_dev;
  208. union {
  209. struct tcphdr *th;
  210. struct udphdr *uh;
  211. struct icmphdr *icmph;
  212. struct igmphdr *igmph;
  213. struct iphdr *ipiph;
  214. struct ipv6hdr *ipv6h;
  215. unsigned char *raw;
  216. } h;
  217. union {
  218. struct iphdr *iph;
  219. struct ipv6hdr *ipv6h;
  220. struct arphdr *arph;
  221. unsigned char *raw;
  222. } nh;
  223. union {
  224. unsigned char *raw;
  225. } mac;
  226. struct dst_entry *dst;
  227. struct sec_path *sp;
  228. /*
  229. * This is the control buffer. It is free to use for every
  230. * layer. Please put your private variables there. If you
  231. * want to keep them across layers you have to do a skb_clone()
  232. * first. This is owned by whoever has the skb queued ATM.
  233. */
  234. char cb[48];
  235. unsigned int len,
  236. data_len,
  237. mac_len,
  238. csum;
  239. __u32 priority;
  240. __u8 local_df:1,
  241. cloned:1,
  242. ip_summed:2,
  243. nohdr:1,
  244. nfctinfo:3;
  245. __u8 pkt_type:3,
  246. fclone:2,
  247. ipvs_property:1;
  248. __be16 protocol;
  249. void (*destructor)(struct sk_buff *skb);
  250. #ifdef CONFIG_NETFILTER
  251. struct nf_conntrack *nfct;
  252. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  253. struct sk_buff *nfct_reasm;
  254. #endif
  255. #ifdef CONFIG_BRIDGE_NETFILTER
  256. struct nf_bridge_info *nf_bridge;
  257. #endif
  258. __u32 nfmark;
  259. #endif /* CONFIG_NETFILTER */
  260. #ifdef CONFIG_NET_SCHED
  261. __u16 tc_index; /* traffic control index */
  262. #ifdef CONFIG_NET_CLS_ACT
  263. __u16 tc_verd; /* traffic control verdict */
  264. #endif
  265. #endif
  266. #ifdef CONFIG_NET_DMA
  267. dma_cookie_t dma_cookie;
  268. #endif
  269. #ifdef CONFIG_NETWORK_SECMARK
  270. __u32 secmark;
  271. #endif
  272. /* These elements must be at the end, see alloc_skb() for details. */
  273. unsigned int truesize;
  274. atomic_t users;
  275. unsigned char *head,
  276. *data,
  277. *tail,
  278. *end;
  279. };
  280. #ifdef __KERNEL__
  281. /*
  282. * Handling routines are only of interest to the kernel
  283. */
  284. #include <linux/slab.h>
  285. #include <asm/system.h>
  286. extern void kfree_skb(struct sk_buff *skb);
  287. extern void __kfree_skb(struct sk_buff *skb);
  288. extern struct sk_buff *__alloc_skb(unsigned int size,
  289. gfp_t priority, int fclone);
  290. static inline struct sk_buff *alloc_skb(unsigned int size,
  291. gfp_t priority)
  292. {
  293. return __alloc_skb(size, priority, 0);
  294. }
  295. static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
  296. gfp_t priority)
  297. {
  298. return __alloc_skb(size, priority, 1);
  299. }
  300. extern struct sk_buff *alloc_skb_from_cache(kmem_cache_t *cp,
  301. unsigned int size,
  302. gfp_t priority);
  303. extern void kfree_skbmem(struct sk_buff *skb);
  304. extern struct sk_buff *skb_clone(struct sk_buff *skb,
  305. gfp_t priority);
  306. extern struct sk_buff *skb_copy(const struct sk_buff *skb,
  307. gfp_t priority);
  308. extern struct sk_buff *pskb_copy(struct sk_buff *skb,
  309. gfp_t gfp_mask);
  310. extern int pskb_expand_head(struct sk_buff *skb,
  311. int nhead, int ntail,
  312. gfp_t gfp_mask);
  313. extern struct sk_buff *skb_realloc_headroom(struct sk_buff *skb,
  314. unsigned int headroom);
  315. extern struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
  316. int newheadroom, int newtailroom,
  317. gfp_t priority);
  318. extern int skb_pad(struct sk_buff *skb, int pad);
  319. #define dev_kfree_skb(a) kfree_skb(a)
  320. extern void skb_over_panic(struct sk_buff *skb, int len,
  321. void *here);
  322. extern void skb_under_panic(struct sk_buff *skb, int len,
  323. void *here);
  324. extern void skb_truesize_bug(struct sk_buff *skb);
  325. static inline void skb_truesize_check(struct sk_buff *skb)
  326. {
  327. if (unlikely((int)skb->truesize < sizeof(struct sk_buff) + skb->len))
  328. skb_truesize_bug(skb);
  329. }
  330. extern int skb_append_datato_frags(struct sock *sk, struct sk_buff *skb,
  331. int getfrag(void *from, char *to, int offset,
  332. int len,int odd, struct sk_buff *skb),
  333. void *from, int length);
  334. struct skb_seq_state
  335. {
  336. __u32 lower_offset;
  337. __u32 upper_offset;
  338. __u32 frag_idx;
  339. __u32 stepped_offset;
  340. struct sk_buff *root_skb;
  341. struct sk_buff *cur_skb;
  342. __u8 *frag_data;
  343. };
  344. extern void skb_prepare_seq_read(struct sk_buff *skb,
  345. unsigned int from, unsigned int to,
  346. struct skb_seq_state *st);
  347. extern unsigned int skb_seq_read(unsigned int consumed, const u8 **data,
  348. struct skb_seq_state *st);
  349. extern void skb_abort_seq_read(struct skb_seq_state *st);
  350. extern unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
  351. unsigned int to, struct ts_config *config,
  352. struct ts_state *state);
  353. /* Internal */
  354. #define skb_shinfo(SKB) ((struct skb_shared_info *)((SKB)->end))
  355. /**
  356. * skb_queue_empty - check if a queue is empty
  357. * @list: queue head
  358. *
  359. * Returns true if the queue is empty, false otherwise.
  360. */
  361. static inline int skb_queue_empty(const struct sk_buff_head *list)
  362. {
  363. return list->next == (struct sk_buff *)list;
  364. }
  365. /**
  366. * skb_get - reference buffer
  367. * @skb: buffer to reference
  368. *
  369. * Makes another reference to a socket buffer and returns a pointer
  370. * to the buffer.
  371. */
  372. static inline struct sk_buff *skb_get(struct sk_buff *skb)
  373. {
  374. atomic_inc(&skb->users);
  375. return skb;
  376. }
  377. /*
  378. * If users == 1, we are the only owner and are can avoid redundant
  379. * atomic change.
  380. */
  381. /**
  382. * skb_cloned - is the buffer a clone
  383. * @skb: buffer to check
  384. *
  385. * Returns true if the buffer was generated with skb_clone() and is
  386. * one of multiple shared copies of the buffer. Cloned buffers are
  387. * shared data so must not be written to under normal circumstances.
  388. */
  389. static inline int skb_cloned(const struct sk_buff *skb)
  390. {
  391. return skb->cloned &&
  392. (atomic_read(&skb_shinfo(skb)->dataref) & SKB_DATAREF_MASK) != 1;
  393. }
  394. /**
  395. * skb_header_cloned - is the header a clone
  396. * @skb: buffer to check
  397. *
  398. * Returns true if modifying the header part of the buffer requires
  399. * the data to be copied.
  400. */
  401. static inline int skb_header_cloned(const struct sk_buff *skb)
  402. {
  403. int dataref;
  404. if (!skb->cloned)
  405. return 0;
  406. dataref = atomic_read(&skb_shinfo(skb)->dataref);
  407. dataref = (dataref & SKB_DATAREF_MASK) - (dataref >> SKB_DATAREF_SHIFT);
  408. return dataref != 1;
  409. }
  410. /**
  411. * skb_header_release - release reference to header
  412. * @skb: buffer to operate on
  413. *
  414. * Drop a reference to the header part of the buffer. This is done
  415. * by acquiring a payload reference. You must not read from the header
  416. * part of skb->data after this.
  417. */
  418. static inline void skb_header_release(struct sk_buff *skb)
  419. {
  420. BUG_ON(skb->nohdr);
  421. skb->nohdr = 1;
  422. atomic_add(1 << SKB_DATAREF_SHIFT, &skb_shinfo(skb)->dataref);
  423. }
  424. /**
  425. * skb_shared - is the buffer shared
  426. * @skb: buffer to check
  427. *
  428. * Returns true if more than one person has a reference to this
  429. * buffer.
  430. */
  431. static inline int skb_shared(const struct sk_buff *skb)
  432. {
  433. return atomic_read(&skb->users) != 1;
  434. }
  435. /**
  436. * skb_share_check - check if buffer is shared and if so clone it
  437. * @skb: buffer to check
  438. * @pri: priority for memory allocation
  439. *
  440. * If the buffer is shared the buffer is cloned and the old copy
  441. * drops a reference. A new clone with a single reference is returned.
  442. * If the buffer is not shared the original buffer is returned. When
  443. * being called from interrupt status or with spinlocks held pri must
  444. * be GFP_ATOMIC.
  445. *
  446. * NULL is returned on a memory allocation failure.
  447. */
  448. static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
  449. gfp_t pri)
  450. {
  451. might_sleep_if(pri & __GFP_WAIT);
  452. if (skb_shared(skb)) {
  453. struct sk_buff *nskb = skb_clone(skb, pri);
  454. kfree_skb(skb);
  455. skb = nskb;
  456. }
  457. return skb;
  458. }
  459. /*
  460. * Copy shared buffers into a new sk_buff. We effectively do COW on
  461. * packets to handle cases where we have a local reader and forward
  462. * and a couple of other messy ones. The normal one is tcpdumping
  463. * a packet thats being forwarded.
  464. */
  465. /**
  466. * skb_unshare - make a copy of a shared buffer
  467. * @skb: buffer to check
  468. * @pri: priority for memory allocation
  469. *
  470. * If the socket buffer is a clone then this function creates a new
  471. * copy of the data, drops a reference count on the old copy and returns
  472. * the new copy with the reference count at 1. If the buffer is not a clone
  473. * the original buffer is returned. When called with a spinlock held or
  474. * from interrupt state @pri must be %GFP_ATOMIC
  475. *
  476. * %NULL is returned on a memory allocation failure.
  477. */
  478. static inline struct sk_buff *skb_unshare(struct sk_buff *skb,
  479. gfp_t pri)
  480. {
  481. might_sleep_if(pri & __GFP_WAIT);
  482. if (skb_cloned(skb)) {
  483. struct sk_buff *nskb = skb_copy(skb, pri);
  484. kfree_skb(skb); /* Free our shared copy */
  485. skb = nskb;
  486. }
  487. return skb;
  488. }
  489. /**
  490. * skb_peek
  491. * @list_: list to peek at
  492. *
  493. * Peek an &sk_buff. Unlike most other operations you _MUST_
  494. * be careful with this one. A peek leaves the buffer on the
  495. * list and someone else may run off with it. You must hold
  496. * the appropriate locks or have a private queue to do this.
  497. *
  498. * Returns %NULL for an empty list or a pointer to the head element.
  499. * The reference count is not incremented and the reference is therefore
  500. * volatile. Use with caution.
  501. */
  502. static inline struct sk_buff *skb_peek(struct sk_buff_head *list_)
  503. {
  504. struct sk_buff *list = ((struct sk_buff *)list_)->next;
  505. if (list == (struct sk_buff *)list_)
  506. list = NULL;
  507. return list;
  508. }
  509. /**
  510. * skb_peek_tail
  511. * @list_: list to peek at
  512. *
  513. * Peek an &sk_buff. Unlike most other operations you _MUST_
  514. * be careful with this one. A peek leaves the buffer on the
  515. * list and someone else may run off with it. You must hold
  516. * the appropriate locks or have a private queue to do this.
  517. *
  518. * Returns %NULL for an empty list or a pointer to the tail element.
  519. * The reference count is not incremented and the reference is therefore
  520. * volatile. Use with caution.
  521. */
  522. static inline struct sk_buff *skb_peek_tail(struct sk_buff_head *list_)
  523. {
  524. struct sk_buff *list = ((struct sk_buff *)list_)->prev;
  525. if (list == (struct sk_buff *)list_)
  526. list = NULL;
  527. return list;
  528. }
  529. /**
  530. * skb_queue_len - get queue length
  531. * @list_: list to measure
  532. *
  533. * Return the length of an &sk_buff queue.
  534. */
  535. static inline __u32 skb_queue_len(const struct sk_buff_head *list_)
  536. {
  537. return list_->qlen;
  538. }
  539. static inline void skb_queue_head_init(struct sk_buff_head *list)
  540. {
  541. spin_lock_init(&list->lock);
  542. list->prev = list->next = (struct sk_buff *)list;
  543. list->qlen = 0;
  544. }
  545. /*
  546. * Insert an sk_buff at the start of a list.
  547. *
  548. * The "__skb_xxxx()" functions are the non-atomic ones that
  549. * can only be called with interrupts disabled.
  550. */
  551. /**
  552. * __skb_queue_after - queue a buffer at the list head
  553. * @list: list to use
  554. * @prev: place after this buffer
  555. * @newsk: buffer to queue
  556. *
  557. * Queue a buffer int the middle of a list. This function takes no locks
  558. * and you must therefore hold required locks before calling it.
  559. *
  560. * A buffer cannot be placed on two lists at the same time.
  561. */
  562. static inline void __skb_queue_after(struct sk_buff_head *list,
  563. struct sk_buff *prev,
  564. struct sk_buff *newsk)
  565. {
  566. struct sk_buff *next;
  567. list->qlen++;
  568. next = prev->next;
  569. newsk->next = next;
  570. newsk->prev = prev;
  571. next->prev = prev->next = newsk;
  572. }
  573. /**
  574. * __skb_queue_head - queue a buffer at the list head
  575. * @list: list to use
  576. * @newsk: buffer to queue
  577. *
  578. * Queue a buffer at the start of a list. This function takes no locks
  579. * and you must therefore hold required locks before calling it.
  580. *
  581. * A buffer cannot be placed on two lists at the same time.
  582. */
  583. extern void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk);
  584. static inline void __skb_queue_head(struct sk_buff_head *list,
  585. struct sk_buff *newsk)
  586. {
  587. __skb_queue_after(list, (struct sk_buff *)list, newsk);
  588. }
  589. /**
  590. * __skb_queue_tail - queue a buffer at the list tail
  591. * @list: list to use
  592. * @newsk: buffer to queue
  593. *
  594. * Queue a buffer at the end of a list. This function takes no locks
  595. * and you must therefore hold required locks before calling it.
  596. *
  597. * A buffer cannot be placed on two lists at the same time.
  598. */
  599. extern void skb_queue_tail(struct sk_buff_head *list, struct sk_buff *newsk);
  600. static inline void __skb_queue_tail(struct sk_buff_head *list,
  601. struct sk_buff *newsk)
  602. {
  603. struct sk_buff *prev, *next;
  604. list->qlen++;
  605. next = (struct sk_buff *)list;
  606. prev = next->prev;
  607. newsk->next = next;
  608. newsk->prev = prev;
  609. next->prev = prev->next = newsk;
  610. }
  611. /**
  612. * __skb_dequeue - remove from the head of the queue
  613. * @list: list to dequeue from
  614. *
  615. * Remove the head of the list. This function does not take any locks
  616. * so must be used with appropriate locks held only. The head item is
  617. * returned or %NULL if the list is empty.
  618. */
  619. extern struct sk_buff *skb_dequeue(struct sk_buff_head *list);
  620. static inline struct sk_buff *__skb_dequeue(struct sk_buff_head *list)
  621. {
  622. struct sk_buff *next, *prev, *result;
  623. prev = (struct sk_buff *) list;
  624. next = prev->next;
  625. result = NULL;
  626. if (next != prev) {
  627. result = next;
  628. next = next->next;
  629. list->qlen--;
  630. next->prev = prev;
  631. prev->next = next;
  632. result->next = result->prev = NULL;
  633. }
  634. return result;
  635. }
  636. /*
  637. * Insert a packet on a list.
  638. */
  639. extern void skb_insert(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
  640. static inline void __skb_insert(struct sk_buff *newsk,
  641. struct sk_buff *prev, struct sk_buff *next,
  642. struct sk_buff_head *list)
  643. {
  644. newsk->next = next;
  645. newsk->prev = prev;
  646. next->prev = prev->next = newsk;
  647. list->qlen++;
  648. }
  649. /*
  650. * Place a packet after a given packet in a list.
  651. */
  652. extern void skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list);
  653. static inline void __skb_append(struct sk_buff *old, struct sk_buff *newsk, struct sk_buff_head *list)
  654. {
  655. __skb_insert(newsk, old, old->next, list);
  656. }
  657. /*
  658. * remove sk_buff from list. _Must_ be called atomically, and with
  659. * the list known..
  660. */
  661. extern void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list);
  662. static inline void __skb_unlink(struct sk_buff *skb, struct sk_buff_head *list)
  663. {
  664. struct sk_buff *next, *prev;
  665. list->qlen--;
  666. next = skb->next;
  667. prev = skb->prev;
  668. skb->next = skb->prev = NULL;
  669. next->prev = prev;
  670. prev->next = next;
  671. }
  672. /* XXX: more streamlined implementation */
  673. /**
  674. * __skb_dequeue_tail - remove from the tail of the queue
  675. * @list: list to dequeue from
  676. *
  677. * Remove the tail of the list. This function does not take any locks
  678. * so must be used with appropriate locks held only. The tail item is
  679. * returned or %NULL if the list is empty.
  680. */
  681. extern struct sk_buff *skb_dequeue_tail(struct sk_buff_head *list);
  682. static inline struct sk_buff *__skb_dequeue_tail(struct sk_buff_head *list)
  683. {
  684. struct sk_buff *skb = skb_peek_tail(list);
  685. if (skb)
  686. __skb_unlink(skb, list);
  687. return skb;
  688. }
  689. static inline int skb_is_nonlinear(const struct sk_buff *skb)
  690. {
  691. return skb->data_len;
  692. }
  693. static inline unsigned int skb_headlen(const struct sk_buff *skb)
  694. {
  695. return skb->len - skb->data_len;
  696. }
  697. static inline int skb_pagelen(const struct sk_buff *skb)
  698. {
  699. int i, len = 0;
  700. for (i = (int)skb_shinfo(skb)->nr_frags - 1; i >= 0; i--)
  701. len += skb_shinfo(skb)->frags[i].size;
  702. return len + skb_headlen(skb);
  703. }
  704. static inline void skb_fill_page_desc(struct sk_buff *skb, int i,
  705. struct page *page, int off, int size)
  706. {
  707. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  708. frag->page = page;
  709. frag->page_offset = off;
  710. frag->size = size;
  711. skb_shinfo(skb)->nr_frags = i + 1;
  712. }
  713. #define SKB_PAGE_ASSERT(skb) BUG_ON(skb_shinfo(skb)->nr_frags)
  714. #define SKB_FRAG_ASSERT(skb) BUG_ON(skb_shinfo(skb)->frag_list)
  715. #define SKB_LINEAR_ASSERT(skb) BUG_ON(skb_is_nonlinear(skb))
  716. /*
  717. * Add data to an sk_buff
  718. */
  719. static inline unsigned char *__skb_put(struct sk_buff *skb, unsigned int len)
  720. {
  721. unsigned char *tmp = skb->tail;
  722. SKB_LINEAR_ASSERT(skb);
  723. skb->tail += len;
  724. skb->len += len;
  725. return tmp;
  726. }
  727. /**
  728. * skb_put - add data to a buffer
  729. * @skb: buffer to use
  730. * @len: amount of data to add
  731. *
  732. * This function extends the used data area of the buffer. If this would
  733. * exceed the total buffer size the kernel will panic. A pointer to the
  734. * first byte of the extra data is returned.
  735. */
  736. static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)
  737. {
  738. unsigned char *tmp = skb->tail;
  739. SKB_LINEAR_ASSERT(skb);
  740. skb->tail += len;
  741. skb->len += len;
  742. if (unlikely(skb->tail>skb->end))
  743. skb_over_panic(skb, len, current_text_addr());
  744. return tmp;
  745. }
  746. static inline unsigned char *__skb_push(struct sk_buff *skb, unsigned int len)
  747. {
  748. skb->data -= len;
  749. skb->len += len;
  750. return skb->data;
  751. }
  752. /**
  753. * skb_push - add data to the start of a buffer
  754. * @skb: buffer to use
  755. * @len: amount of data to add
  756. *
  757. * This function extends the used data area of the buffer at the buffer
  758. * start. If this would exceed the total buffer headroom the kernel will
  759. * panic. A pointer to the first byte of the extra data is returned.
  760. */
  761. static inline unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
  762. {
  763. skb->data -= len;
  764. skb->len += len;
  765. if (unlikely(skb->data<skb->head))
  766. skb_under_panic(skb, len, current_text_addr());
  767. return skb->data;
  768. }
  769. static inline unsigned char *__skb_pull(struct sk_buff *skb, unsigned int len)
  770. {
  771. skb->len -= len;
  772. BUG_ON(skb->len < skb->data_len);
  773. return skb->data += len;
  774. }
  775. /**
  776. * skb_pull - remove data from the start of a buffer
  777. * @skb: buffer to use
  778. * @len: amount of data to remove
  779. *
  780. * This function removes data from the start of a buffer, returning
  781. * the memory to the headroom. A pointer to the next data in the buffer
  782. * is returned. Once the data has been pulled future pushes will overwrite
  783. * the old data.
  784. */
  785. static inline unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
  786. {
  787. return unlikely(len > skb->len) ? NULL : __skb_pull(skb, len);
  788. }
  789. extern unsigned char *__pskb_pull_tail(struct sk_buff *skb, int delta);
  790. static inline unsigned char *__pskb_pull(struct sk_buff *skb, unsigned int len)
  791. {
  792. if (len > skb_headlen(skb) &&
  793. !__pskb_pull_tail(skb, len-skb_headlen(skb)))
  794. return NULL;
  795. skb->len -= len;
  796. return skb->data += len;
  797. }
  798. static inline unsigned char *pskb_pull(struct sk_buff *skb, unsigned int len)
  799. {
  800. return unlikely(len > skb->len) ? NULL : __pskb_pull(skb, len);
  801. }
  802. static inline int pskb_may_pull(struct sk_buff *skb, unsigned int len)
  803. {
  804. if (likely(len <= skb_headlen(skb)))
  805. return 1;
  806. if (unlikely(len > skb->len))
  807. return 0;
  808. return __pskb_pull_tail(skb, len-skb_headlen(skb)) != NULL;
  809. }
  810. /**
  811. * skb_headroom - bytes at buffer head
  812. * @skb: buffer to check
  813. *
  814. * Return the number of bytes of free space at the head of an &sk_buff.
  815. */
  816. static inline int skb_headroom(const struct sk_buff *skb)
  817. {
  818. return skb->data - skb->head;
  819. }
  820. /**
  821. * skb_tailroom - bytes at buffer end
  822. * @skb: buffer to check
  823. *
  824. * Return the number of bytes of free space at the tail of an sk_buff
  825. */
  826. static inline int skb_tailroom(const struct sk_buff *skb)
  827. {
  828. return skb_is_nonlinear(skb) ? 0 : skb->end - skb->tail;
  829. }
  830. /**
  831. * skb_reserve - adjust headroom
  832. * @skb: buffer to alter
  833. * @len: bytes to move
  834. *
  835. * Increase the headroom of an empty &sk_buff by reducing the tail
  836. * room. This is only allowed for an empty buffer.
  837. */
  838. static inline void skb_reserve(struct sk_buff *skb, int len)
  839. {
  840. skb->data += len;
  841. skb->tail += len;
  842. }
  843. /*
  844. * CPUs often take a performance hit when accessing unaligned memory
  845. * locations. The actual performance hit varies, it can be small if the
  846. * hardware handles it or large if we have to take an exception and fix it
  847. * in software.
  848. *
  849. * Since an ethernet header is 14 bytes network drivers often end up with
  850. * the IP header at an unaligned offset. The IP header can be aligned by
  851. * shifting the start of the packet by 2 bytes. Drivers should do this
  852. * with:
  853. *
  854. * skb_reserve(NET_IP_ALIGN);
  855. *
  856. * The downside to this alignment of the IP header is that the DMA is now
  857. * unaligned. On some architectures the cost of an unaligned DMA is high
  858. * and this cost outweighs the gains made by aligning the IP header.
  859. *
  860. * Since this trade off varies between architectures, we allow NET_IP_ALIGN
  861. * to be overridden.
  862. */
  863. #ifndef NET_IP_ALIGN
  864. #define NET_IP_ALIGN 2
  865. #endif
  866. /*
  867. * The networking layer reserves some headroom in skb data (via
  868. * dev_alloc_skb). This is used to avoid having to reallocate skb data when
  869. * the header has to grow. In the default case, if the header has to grow
  870. * 16 bytes or less we avoid the reallocation.
  871. *
  872. * Unfortunately this headroom changes the DMA alignment of the resulting
  873. * network packet. As for NET_IP_ALIGN, this unaligned DMA is expensive
  874. * on some architectures. An architecture can override this value,
  875. * perhaps setting it to a cacheline in size (since that will maintain
  876. * cacheline alignment of the DMA). It must be a power of 2.
  877. *
  878. * Various parts of the networking layer expect at least 16 bytes of
  879. * headroom, you should not reduce this.
  880. */
  881. #ifndef NET_SKB_PAD
  882. #define NET_SKB_PAD 16
  883. #endif
  884. extern int ___pskb_trim(struct sk_buff *skb, unsigned int len);
  885. static inline void __skb_trim(struct sk_buff *skb, unsigned int len)
  886. {
  887. if (unlikely(skb->data_len)) {
  888. WARN_ON(1);
  889. return;
  890. }
  891. skb->len = len;
  892. skb->tail = skb->data + len;
  893. }
  894. /**
  895. * skb_trim - remove end from a buffer
  896. * @skb: buffer to alter
  897. * @len: new length
  898. *
  899. * Cut the length of a buffer down by removing data from the tail. If
  900. * the buffer is already under the length specified it is not modified.
  901. * The skb must be linear.
  902. */
  903. static inline void skb_trim(struct sk_buff *skb, unsigned int len)
  904. {
  905. if (skb->len > len)
  906. __skb_trim(skb, len);
  907. }
  908. static inline int __pskb_trim(struct sk_buff *skb, unsigned int len)
  909. {
  910. if (skb->data_len)
  911. return ___pskb_trim(skb, len);
  912. __skb_trim(skb, len);
  913. return 0;
  914. }
  915. static inline int pskb_trim(struct sk_buff *skb, unsigned int len)
  916. {
  917. return (len < skb->len) ? __pskb_trim(skb, len) : 0;
  918. }
  919. /**
  920. * skb_orphan - orphan a buffer
  921. * @skb: buffer to orphan
  922. *
  923. * If a buffer currently has an owner then we call the owner's
  924. * destructor function and make the @skb unowned. The buffer continues
  925. * to exist but is no longer charged to its former owner.
  926. */
  927. static inline void skb_orphan(struct sk_buff *skb)
  928. {
  929. if (skb->destructor)
  930. skb->destructor(skb);
  931. skb->destructor = NULL;
  932. skb->sk = NULL;
  933. }
  934. /**
  935. * __skb_queue_purge - empty a list
  936. * @list: list to empty
  937. *
  938. * Delete all buffers on an &sk_buff list. Each buffer is removed from
  939. * the list and one reference dropped. This function does not take the
  940. * list lock and the caller must hold the relevant locks to use it.
  941. */
  942. extern void skb_queue_purge(struct sk_buff_head *list);
  943. static inline void __skb_queue_purge(struct sk_buff_head *list)
  944. {
  945. struct sk_buff *skb;
  946. while ((skb = __skb_dequeue(list)) != NULL)
  947. kfree_skb(skb);
  948. }
  949. #ifndef CONFIG_HAVE_ARCH_DEV_ALLOC_SKB
  950. /**
  951. * __dev_alloc_skb - allocate an skbuff for sending
  952. * @length: length to allocate
  953. * @gfp_mask: get_free_pages mask, passed to alloc_skb
  954. *
  955. * Allocate a new &sk_buff and assign it a usage count of one. The
  956. * buffer has unspecified headroom built in. Users should allocate
  957. * the headroom they think they need without accounting for the
  958. * built in space. The built in space is used for optimisations.
  959. *
  960. * %NULL is returned in there is no free memory.
  961. */
  962. static inline struct sk_buff *__dev_alloc_skb(unsigned int length,
  963. gfp_t gfp_mask)
  964. {
  965. struct sk_buff *skb = alloc_skb(length + NET_SKB_PAD, gfp_mask);
  966. if (likely(skb))
  967. skb_reserve(skb, NET_SKB_PAD);
  968. return skb;
  969. }
  970. #else
  971. extern struct sk_buff *__dev_alloc_skb(unsigned int length, int gfp_mask);
  972. #endif
  973. /**
  974. * dev_alloc_skb - allocate an skbuff for sending
  975. * @length: length to allocate
  976. *
  977. * Allocate a new &sk_buff and assign it a usage count of one. The
  978. * buffer has unspecified headroom built in. Users should allocate
  979. * the headroom they think they need without accounting for the
  980. * built in space. The built in space is used for optimisations.
  981. *
  982. * %NULL is returned in there is no free memory. Although this function
  983. * allocates memory it can be called from an interrupt.
  984. */
  985. static inline struct sk_buff *dev_alloc_skb(unsigned int length)
  986. {
  987. return __dev_alloc_skb(length, GFP_ATOMIC);
  988. }
  989. /**
  990. * skb_cow - copy header of skb when it is required
  991. * @skb: buffer to cow
  992. * @headroom: needed headroom
  993. *
  994. * If the skb passed lacks sufficient headroom or its data part
  995. * is shared, data is reallocated. If reallocation fails, an error
  996. * is returned and original skb is not changed.
  997. *
  998. * The result is skb with writable area skb->head...skb->tail
  999. * and at least @headroom of space at head.
  1000. */
  1001. static inline int skb_cow(struct sk_buff *skb, unsigned int headroom)
  1002. {
  1003. int delta = (headroom > NET_SKB_PAD ? headroom : NET_SKB_PAD) -
  1004. skb_headroom(skb);
  1005. if (delta < 0)
  1006. delta = 0;
  1007. if (delta || skb_cloned(skb))
  1008. return pskb_expand_head(skb, (delta + (NET_SKB_PAD-1)) &
  1009. ~(NET_SKB_PAD-1), 0, GFP_ATOMIC);
  1010. return 0;
  1011. }
  1012. /**
  1013. * skb_padto - pad an skbuff up to a minimal size
  1014. * @skb: buffer to pad
  1015. * @len: minimal length
  1016. *
  1017. * Pads up a buffer to ensure the trailing bytes exist and are
  1018. * blanked. If the buffer already contains sufficient data it
  1019. * is untouched. Otherwise it is extended. Returns zero on
  1020. * success. The skb is freed on error.
  1021. */
  1022. static inline int skb_padto(struct sk_buff *skb, unsigned int len)
  1023. {
  1024. unsigned int size = skb->len;
  1025. if (likely(size >= len))
  1026. return 0;
  1027. return skb_pad(skb, len-size);
  1028. }
  1029. static inline int skb_add_data(struct sk_buff *skb,
  1030. char __user *from, int copy)
  1031. {
  1032. const int off = skb->len;
  1033. if (skb->ip_summed == CHECKSUM_NONE) {
  1034. int err = 0;
  1035. unsigned int csum = csum_and_copy_from_user(from,
  1036. skb_put(skb, copy),
  1037. copy, 0, &err);
  1038. if (!err) {
  1039. skb->csum = csum_block_add(skb->csum, csum, off);
  1040. return 0;
  1041. }
  1042. } else if (!copy_from_user(skb_put(skb, copy), from, copy))
  1043. return 0;
  1044. __skb_trim(skb, off);
  1045. return -EFAULT;
  1046. }
  1047. static inline int skb_can_coalesce(struct sk_buff *skb, int i,
  1048. struct page *page, int off)
  1049. {
  1050. if (i) {
  1051. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i - 1];
  1052. return page == frag->page &&
  1053. off == frag->page_offset + frag->size;
  1054. }
  1055. return 0;
  1056. }
  1057. static inline int __skb_linearize(struct sk_buff *skb)
  1058. {
  1059. return __pskb_pull_tail(skb, skb->data_len) ? 0 : -ENOMEM;
  1060. }
  1061. /**
  1062. * skb_linearize - convert paged skb to linear one
  1063. * @skb: buffer to linarize
  1064. *
  1065. * If there is no free memory -ENOMEM is returned, otherwise zero
  1066. * is returned and the old skb data released.
  1067. */
  1068. static inline int skb_linearize(struct sk_buff *skb)
  1069. {
  1070. return skb_is_nonlinear(skb) ? __skb_linearize(skb) : 0;
  1071. }
  1072. /**
  1073. * skb_linearize_cow - make sure skb is linear and writable
  1074. * @skb: buffer to process
  1075. *
  1076. * If there is no free memory -ENOMEM is returned, otherwise zero
  1077. * is returned and the old skb data released.
  1078. */
  1079. static inline int skb_linearize_cow(struct sk_buff *skb)
  1080. {
  1081. return skb_is_nonlinear(skb) || skb_cloned(skb) ?
  1082. __skb_linearize(skb) : 0;
  1083. }
  1084. /**
  1085. * skb_postpull_rcsum - update checksum for received skb after pull
  1086. * @skb: buffer to update
  1087. * @start: start of data before pull
  1088. * @len: length of data pulled
  1089. *
  1090. * After doing a pull on a received packet, you need to call this to
  1091. * update the CHECKSUM_HW checksum, or set ip_summed to CHECKSUM_NONE
  1092. * so that it can be recomputed from scratch.
  1093. */
  1094. static inline void skb_postpull_rcsum(struct sk_buff *skb,
  1095. const void *start, unsigned int len)
  1096. {
  1097. if (skb->ip_summed == CHECKSUM_HW)
  1098. skb->csum = csum_sub(skb->csum, csum_partial(start, len, 0));
  1099. }
  1100. unsigned char *skb_pull_rcsum(struct sk_buff *skb, unsigned int len);
  1101. /**
  1102. * pskb_trim_rcsum - trim received skb and update checksum
  1103. * @skb: buffer to trim
  1104. * @len: new length
  1105. *
  1106. * This is exactly the same as pskb_trim except that it ensures the
  1107. * checksum of received packets are still valid after the operation.
  1108. */
  1109. static inline int pskb_trim_rcsum(struct sk_buff *skb, unsigned int len)
  1110. {
  1111. if (likely(len >= skb->len))
  1112. return 0;
  1113. if (skb->ip_summed == CHECKSUM_HW)
  1114. skb->ip_summed = CHECKSUM_NONE;
  1115. return __pskb_trim(skb, len);
  1116. }
  1117. static inline void *kmap_skb_frag(const skb_frag_t *frag)
  1118. {
  1119. #ifdef CONFIG_HIGHMEM
  1120. BUG_ON(in_irq());
  1121. local_bh_disable();
  1122. #endif
  1123. return kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ);
  1124. }
  1125. static inline void kunmap_skb_frag(void *vaddr)
  1126. {
  1127. kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
  1128. #ifdef CONFIG_HIGHMEM
  1129. local_bh_enable();
  1130. #endif
  1131. }
  1132. #define skb_queue_walk(queue, skb) \
  1133. for (skb = (queue)->next; \
  1134. prefetch(skb->next), (skb != (struct sk_buff *)(queue)); \
  1135. skb = skb->next)
  1136. #define skb_queue_reverse_walk(queue, skb) \
  1137. for (skb = (queue)->prev; \
  1138. prefetch(skb->prev), (skb != (struct sk_buff *)(queue)); \
  1139. skb = skb->prev)
  1140. extern struct sk_buff *skb_recv_datagram(struct sock *sk, unsigned flags,
  1141. int noblock, int *err);
  1142. extern unsigned int datagram_poll(struct file *file, struct socket *sock,
  1143. struct poll_table_struct *wait);
  1144. extern int skb_copy_datagram_iovec(const struct sk_buff *from,
  1145. int offset, struct iovec *to,
  1146. int size);
  1147. extern int skb_copy_and_csum_datagram_iovec(struct sk_buff *skb,
  1148. int hlen,
  1149. struct iovec *iov);
  1150. extern void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
  1151. extern void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
  1152. unsigned int flags);
  1153. extern unsigned int skb_checksum(const struct sk_buff *skb, int offset,
  1154. int len, unsigned int csum);
  1155. extern int skb_copy_bits(const struct sk_buff *skb, int offset,
  1156. void *to, int len);
  1157. extern int skb_store_bits(const struct sk_buff *skb, int offset,
  1158. void *from, int len);
  1159. extern unsigned int skb_copy_and_csum_bits(const struct sk_buff *skb,
  1160. int offset, u8 *to, int len,
  1161. unsigned int csum);
  1162. extern void skb_copy_and_csum_dev(const struct sk_buff *skb, u8 *to);
  1163. extern void skb_split(struct sk_buff *skb,
  1164. struct sk_buff *skb1, const u32 len);
  1165. extern void skb_release_data(struct sk_buff *skb);
  1166. extern struct sk_buff *skb_segment(struct sk_buff *skb, int features);
  1167. static inline void *skb_header_pointer(const struct sk_buff *skb, int offset,
  1168. int len, void *buffer)
  1169. {
  1170. int hlen = skb_headlen(skb);
  1171. if (hlen - offset >= len)
  1172. return skb->data + offset;
  1173. if (skb_copy_bits(skb, offset, buffer, len) < 0)
  1174. return NULL;
  1175. return buffer;
  1176. }
  1177. extern void skb_init(void);
  1178. extern void skb_add_mtu(int mtu);
  1179. /**
  1180. * skb_get_timestamp - get timestamp from a skb
  1181. * @skb: skb to get stamp from
  1182. * @stamp: pointer to struct timeval to store stamp in
  1183. *
  1184. * Timestamps are stored in the skb as offsets to a base timestamp.
  1185. * This function converts the offset back to a struct timeval and stores
  1186. * it in stamp.
  1187. */
  1188. static inline void skb_get_timestamp(const struct sk_buff *skb, struct timeval *stamp)
  1189. {
  1190. stamp->tv_sec = skb->tstamp.off_sec;
  1191. stamp->tv_usec = skb->tstamp.off_usec;
  1192. }
  1193. /**
  1194. * skb_set_timestamp - set timestamp of a skb
  1195. * @skb: skb to set stamp of
  1196. * @stamp: pointer to struct timeval to get stamp from
  1197. *
  1198. * Timestamps are stored in the skb as offsets to a base timestamp.
  1199. * This function converts a struct timeval to an offset and stores
  1200. * it in the skb.
  1201. */
  1202. static inline void skb_set_timestamp(struct sk_buff *skb, const struct timeval *stamp)
  1203. {
  1204. skb->tstamp.off_sec = stamp->tv_sec;
  1205. skb->tstamp.off_usec = stamp->tv_usec;
  1206. }
  1207. extern void __net_timestamp(struct sk_buff *skb);
  1208. extern unsigned int __skb_checksum_complete(struct sk_buff *skb);
  1209. /**
  1210. * skb_checksum_complete - Calculate checksum of an entire packet
  1211. * @skb: packet to process
  1212. *
  1213. * This function calculates the checksum over the entire packet plus
  1214. * the value of skb->csum. The latter can be used to supply the
  1215. * checksum of a pseudo header as used by TCP/UDP. It returns the
  1216. * checksum.
  1217. *
  1218. * For protocols that contain complete checksums such as ICMP/TCP/UDP,
  1219. * this function can be used to verify that checksum on received
  1220. * packets. In that case the function should return zero if the
  1221. * checksum is correct. In particular, this function will return zero
  1222. * if skb->ip_summed is CHECKSUM_UNNECESSARY which indicates that the
  1223. * hardware has already verified the correctness of the checksum.
  1224. */
  1225. static inline unsigned int skb_checksum_complete(struct sk_buff *skb)
  1226. {
  1227. return skb->ip_summed != CHECKSUM_UNNECESSARY &&
  1228. __skb_checksum_complete(skb);
  1229. }
  1230. #ifdef CONFIG_NETFILTER
  1231. static inline void nf_conntrack_put(struct nf_conntrack *nfct)
  1232. {
  1233. if (nfct && atomic_dec_and_test(&nfct->use))
  1234. nfct->destroy(nfct);
  1235. }
  1236. static inline void nf_conntrack_get(struct nf_conntrack *nfct)
  1237. {
  1238. if (nfct)
  1239. atomic_inc(&nfct->use);
  1240. }
  1241. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1242. static inline void nf_conntrack_get_reasm(struct sk_buff *skb)
  1243. {
  1244. if (skb)
  1245. atomic_inc(&skb->users);
  1246. }
  1247. static inline void nf_conntrack_put_reasm(struct sk_buff *skb)
  1248. {
  1249. if (skb)
  1250. kfree_skb(skb);
  1251. }
  1252. #endif
  1253. #ifdef CONFIG_BRIDGE_NETFILTER
  1254. static inline void nf_bridge_put(struct nf_bridge_info *nf_bridge)
  1255. {
  1256. if (nf_bridge && atomic_dec_and_test(&nf_bridge->use))
  1257. kfree(nf_bridge);
  1258. }
  1259. static inline void nf_bridge_get(struct nf_bridge_info *nf_bridge)
  1260. {
  1261. if (nf_bridge)
  1262. atomic_inc(&nf_bridge->use);
  1263. }
  1264. #endif /* CONFIG_BRIDGE_NETFILTER */
  1265. static inline void nf_reset(struct sk_buff *skb)
  1266. {
  1267. nf_conntrack_put(skb->nfct);
  1268. skb->nfct = NULL;
  1269. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  1270. nf_conntrack_put_reasm(skb->nfct_reasm);
  1271. skb->nfct_reasm = NULL;
  1272. #endif
  1273. #ifdef CONFIG_BRIDGE_NETFILTER
  1274. nf_bridge_put(skb->nf_bridge);
  1275. skb->nf_bridge = NULL;
  1276. #endif
  1277. }
  1278. #else /* CONFIG_NETFILTER */
  1279. static inline void nf_reset(struct sk_buff *skb) {}
  1280. #endif /* CONFIG_NETFILTER */
  1281. #ifdef CONFIG_NETWORK_SECMARK
  1282. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  1283. {
  1284. to->secmark = from->secmark;
  1285. }
  1286. static inline void skb_init_secmark(struct sk_buff *skb)
  1287. {
  1288. skb->secmark = 0;
  1289. }
  1290. #else
  1291. static inline void skb_copy_secmark(struct sk_buff *to, const struct sk_buff *from)
  1292. { }
  1293. static inline void skb_init_secmark(struct sk_buff *skb)
  1294. { }
  1295. #endif
  1296. #endif /* __KERNEL__ */
  1297. #endif /* _LINUX_SKBUFF_H */