skbuff.h 41 KB

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