skbuff.h 34 KB

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