skbuff.h 33 KB

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