pf: Update packetfilter to OpenBSD 4.4
[dragonfly.git] / sys / sys / mbuf.h
1 /*
2  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
3  *
4  * Copyright (c) 1982, 1986, 1988, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)mbuf.h      8.5 (Berkeley) 2/19/95
36  * $FreeBSD: src/sys/sys/mbuf.h,v 1.44.2.17 2003/04/15 06:15:02 silby Exp $
37  * $DragonFly: src/sys/sys/mbuf.h,v 1.54 2008/10/19 08:39:55 sephe Exp $
38  */
39
40 #ifndef _SYS_MBUF_H_
41 #define _SYS_MBUF_H_
42
43 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
44
45 #ifndef _SYS_TYPES_H_
46 #include <sys/types.h>
47 #endif
48 #ifndef _SYS_PARAM_H_
49 #include <sys/param.h>
50 #endif
51 #ifndef _SYS_QUEUE_H_
52 #include <sys/queue.h>
53 #endif
54 #ifndef _NET_NETISR_H_
55 #include <net/netisr.h>
56 #endif
57
58 /*
59  * Mbufs are of a single size, MSIZE (machine/param.h), which
60  * includes overhead.  An mbuf may add a single "mbuf cluster" of size
61  * MCLBYTES (also in machine/param.h), which has no additional overhead
62  * and is used instead of the internal data area; this is done when
63  * at least MINCLSIZE of data must be stored.
64  */
65 #define MLEN            (MSIZE - sizeof(struct m_hdr))  /* normal data len */
66 #define MHLEN           (MLEN - sizeof(struct pkthdr))  /* data len w/pkthdr */
67 #define MINCLSIZE       (MHLEN + 1)     /* smallest amount to put in cluster */
68 #define M_MAXCOMPRESS   (MHLEN / 2)     /* max amount to copy for compression */
69
70 /*
71  * Macros for type conversion:
72  * mtod(m, t)           -- Convert mbuf pointer to data pointer of correct type.
73  * mtodoff(m, t, off)   -- Convert mbuf pointer at the specified offset to data
74  *                         pointer of correct type.
75  */
76 #define mtod(m, t)              ((t)((m)->m_data))
77 #define mtodoff(m, t, off)      ((t)((m)->m_data + (off)))
78
79 /*
80  * Header present at the beginning of every mbuf.
81  */
82 struct m_hdr {
83         struct  mbuf *mh_next;          /* next buffer in chain */
84         struct  mbuf *mh_nextpkt;       /* next chain in queue/record */
85         caddr_t mh_data;                /* location of data */
86         int     mh_len;                 /* amount of data in this mbuf */
87         int     mh_flags;               /* flags; see below */
88         short   mh_type;                /* type of data in this mbuf */
89         short   mh_pad;                 /* padding */
90 #ifdef MBUF_DEBUG
91         const char *mh_lastfunc;
92 #endif
93         struct netmsg_packet mh_netmsg; /* hardware->proto stack msg */
94 };
95
96 /* pf stuff */
97 struct pkthdr_pf {
98         void            *hdr;           /* saved hdr pos in mbuf, for ECN */
99         void            *statekey;      /* pf stackside statekey */
100         u_int            rtableid;      /* alternate routing table id */
101         u_int32_t        qid;           /* queue id */
102         u_int16_t        tag;           /* tag id */
103         u_int8_t         flags;
104         u_int8_t         routed;
105         uint32_t        state_hash;     /* identifies 'connections' */
106         uint8_t         ecn_af;         /* for altq_red */
107         uint8_t         unused01;
108         uint8_t         unused02;
109         uint8_t         unused03;
110 };
111
112 /* pkthdr_pf.flags */
113 #define PF_TAG_GENERATED                0x01
114 #define PF_TAG_FRAGCACHE                0x02
115 #define PF_TAG_TRANSLATE_LOCALHOST      0x04
116 #define PF_TAG_DIVERTED                 0x08
117 #define PF_TAG_DIVERTED_PACKET          0x10
118 #define PF_TAG_REROUTE                  0x20
119
120
121 /*
122  * Packet tag structure (see below for details).
123  */
124 struct m_tag {
125         SLIST_ENTRY(m_tag)      m_tag_link;     /* List of packet tags */
126         u_int16_t               m_tag_id;       /* Tag ID */
127         u_int16_t               m_tag_len;      /* Length of data */
128         u_int32_t               m_tag_cookie;   /* ABI/Module ID */
129 };
130
131 SLIST_HEAD(packet_tags, m_tag);
132
133 /*
134  * Record/packet header in first mbuf of chain; valid only if M_PKTHDR is set.
135  *
136  * Be careful: The fields have been carefully ordered to avoid hidden padding.
137  *             Keep this in mind, when adding or removing fields!
138  */
139 struct pkthdr {
140         struct  ifnet *rcvif;           /* rcv interface */
141         int     len;                    /* total packet length */
142         struct packet_tags tags;        /* list of packet tags */
143
144         /* variables for ip and tcp reassembly */
145         void    *header;                /* pointer to packet header */
146
147         /* variables for hardware checksum */
148         int     csum_flags;             /* flags regarding checksum */
149         int     csum_data;              /* data field used by csum routines */
150
151         /* firewall flags */
152         uint32_t fw_flags;              /* flags for PF */
153
154         /* variables for PF processing */
155         struct pkthdr_pf pf;            /* structure for PF */
156
157         uint16_t ether_vlantag;         /* ethernet 802.1p+q vlan tag */
158         uint16_t hash;                  /* packet hash */
159
160         uint16_t wlan_seqno;            /* IEEE 802.11 seq no. */
161 };
162
163 /*
164  * Description of external storage mapped into mbuf; valid only if M_EXT is set.
165  */
166 struct m_ext {
167         caddr_t ext_buf;                /* start of buffer */
168         void    (*ext_free)(void *);
169         u_int   ext_size;               /* size of buffer, for ext_free */
170         void    (*ext_ref)(void *);
171         void    *ext_arg;
172 };
173
174 /*
175  * The core of the mbuf object along with some shortcut defines for
176  * practical purposes.
177  */
178 struct mbuf {
179         struct  m_hdr m_hdr;
180         union {
181                 struct {
182                         struct  pkthdr MH_pkthdr;       /* M_PKTHDR set */
183                         union {
184                                 struct  m_ext MH_ext;   /* M_EXT set */
185                                 char    MH_databuf[MHLEN];
186                         } MH_dat;
187                 } MH;
188                 char    M_databuf[MLEN];                /* !M_PKTHDR, !M_EXT */
189         } M_dat;
190 };
191 #define m_next          m_hdr.mh_next
192 #define m_len           m_hdr.mh_len
193 #define m_data          m_hdr.mh_data
194 #define m_type          m_hdr.mh_type
195 #define m_flags         m_hdr.mh_flags
196 #define m_nextpkt       m_hdr.mh_nextpkt
197 #define m_pkthdr        M_dat.MH.MH_pkthdr
198 #define m_ext           M_dat.MH.MH_dat.MH_ext
199 #define m_pktdat        M_dat.MH.MH_dat.MH_databuf
200 #define m_dat           M_dat.M_databuf
201
202 /*
203  * Code that uses m_act should be converted to use m_nextpkt
204  * instead; m_act is historical and deprecated.
205  */
206 #define m_act           m_nextpkt
207
208 /*
209  * mbuf flags.
210  */
211 #define M_EXT           0x0001  /* has associated external storage */
212 #define M_PKTHDR        0x0002  /* start of record */
213 #define M_EOR           0x0004  /* end of record */
214 #define M_PROTO1        0x0008  /* protocol-specific */
215 #define M_PROTO2        0x0010  /* protocol-specific */
216 #define M_PROTO3        0x0020  /* protocol-specific */
217 #define M_PROTO4        0x0040  /* protocol-specific */
218 #define M_PROTO5        0x0080  /* protocol-specific */
219
220 /*
221  * mbuf pkthdr flags (also stored in m_flags).
222  */
223 #define M_BCAST         0x0100  /* send/received as link-level broadcast */
224 #define M_MCAST         0x0200  /* send/received as link-level multicast */
225 #define M_FRAG          0x0400  /* packet is a fragment of a larger packet */
226 #define M_FIRSTFRAG     0x0800  /* packet is first fragment */
227 #define M_LASTFRAG      0x1000  /* packet is last fragment */
228 #define M_CLCACHE       0x2000  /* mbuf allocated from the cluster cache */
229 #define M_EXT_CLUSTER   0x4000  /* standard cluster else special */
230 #define M_PHCACHE       0x8000  /* mbuf allocated from the pkt header cache */
231 #define M_NOTIFICATION  0x10000 /* notification event */
232 #define M_VLANTAG       0x20000 /* ether_vlantag is valid */
233 #define M_MPLSLABELED   0x40000 /* packet is mpls labeled */
234 #define M_LENCHECKED    0x80000 /* packet proto lengths are checked */
235 #define M_HASH          0x100000/* hash field in pkthdr is valid */
236 #define M_PROTO6        0x200000/* protocol-specific */
237 #define M_PROTO7        0x400000/* protocol-specific */
238 #define M_PROTO8        0x800000/* protocol-specific */
239
240 /*
241  * Flags copied when copying m_pkthdr.
242  */
243 #define M_COPYFLAGS     (M_PKTHDR|M_EOR|M_PROTO1|M_PROTO2|M_PROTO3 | \
244                          M_PROTO4|M_PROTO5|M_PROTO6|M_PROTO7|M_PROTO8 | \
245                          M_BCAST|M_MCAST|M_FRAG|M_FIRSTFRAG|M_LASTFRAG | \
246                          M_VLANTAG|M_MPLSLABELED | \
247                          M_LENCHECKED|M_HASH)
248
249 /*
250  * Flags indicating hw checksum support and sw checksum requirements.
251  */
252 #define CSUM_IP                 0x0001          /* will csum IP */
253 #define CSUM_TCP                0x0002          /* will csum TCP */
254 #define CSUM_UDP                0x0004          /* will csum UDP */
255 #define CSUM_IP_FRAGS           0x0008          /* will csum IP fragments */
256 #define CSUM_FRAGMENT           0x0010          /* will do IP fragmentation */
257
258 #define CSUM_IP_CHECKED         0x0100          /* did csum IP */
259 #define CSUM_IP_VALID           0x0200          /*   ... the csum is valid */
260 #define CSUM_DATA_VALID         0x0400          /* csum_data field is valid */
261 #define CSUM_PSEUDO_HDR         0x0800          /* csum_data has pseudo hdr */
262 #define CSUM_FRAG_NOT_CHECKED   0x1000          /* did _not_ csum fragment
263                                                  * NB: This flag is only used
264                                                  * by IP defragmenter.
265                                                  */
266
267 #define CSUM_DELAY_DATA         (CSUM_TCP | CSUM_UDP)
268 #define CSUM_DELAY_IP           (CSUM_IP)       /* XXX add ipv6 here too? */
269
270 /*
271  * Flags indicating PF processing status
272  */
273 #define FW_MBUF_GENERATED       0x00000001
274 #define PF_MBUF_STRUCTURE       0x00000002      /* m_pkthdr.pf valid */
275 #define PF_MBUF_ROUTED          0x00000004      /* pf_routed field is valid */
276 #define PF_MBUF_TAGGED          0x00000008
277 #define XX_MBUF_UNUSED10        0x00000010
278 #define XX_MBUF_UNUSED20        0x00000020
279 #define IPFORWARD_MBUF_TAGGED   0x00000040
280 #define DUMMYNET_MBUF_TAGGED    0x00000080
281 #define XX_MBUF_UNUSED100       0x00000100
282 #define FW_MBUF_REDISPATCH      0x00000200
283 #define IPFW_MBUF_GENERATED     FW_MBUF_GENERATED
284 /*
285  * mbuf types.
286  */
287 #define MT_FREE         0       /* should be on free list */
288 #define MT_DATA         1       /* dynamic (data) allocation */
289 #define MT_HEADER       2       /* packet header */
290 #define MT_SONAME       3       /* socket name */
291 /* 4 was MT_TAG */
292 #define MT_CONTROL      5       /* extra-data protocol message */
293 #define MT_OOBDATA      6       /* expedited data  */
294 #define MT_NTYPES       7       /* number of mbuf types for mbtypes[] */
295
296 struct mbuf_chain {
297         struct mbuf     *mc_head;
298         struct mbuf     *mc_tail;
299 };
300
301 /*
302  * General mbuf allocator statistics structure.
303  */
304 struct mbstat {
305         u_long  m_mbufs;        /* mbufs obtained from page pool */
306         u_long  m_clusters;     /* clusters obtained from page pool */
307         u_long  m_spare;        /* spare field */
308         u_long  m_clfree;       /* free clusters */
309         u_long  m_drops;        /* times failed to find space */
310         u_long  m_wait;         /* times waited for space */
311         u_long  m_drain;        /* times drained protocols for space */
312         u_long  m_mcfail;       /* times m_copym failed */
313         u_long  m_mpfail;       /* times m_pullup failed */
314         u_long  m_msize;        /* length of an mbuf */
315         u_long  m_mclbytes;     /* length of an mbuf cluster */
316         u_long  m_minclsize;    /* min length of data to allocate a cluster */
317         u_long  m_mlen;         /* length of data in an mbuf */
318         u_long  m_mhlen;        /* length of data in a header mbuf */
319 };
320
321 /*
322  * Flags specifying how an allocation should be made.
323  */
324
325 #define MB_DONTWAIT     0x4
326 #define MB_TRYWAIT      0x8
327 #define MB_WAIT         MB_TRYWAIT
328
329 /*
330  * Mbuf to Malloc Flag Conversion.
331  */
332 #define MBTOM(how)      ((how) & MB_TRYWAIT ? M_WAITOK : M_NOWAIT)
333
334 /*
335  * These are identifying numbers passed to the m_mballoc_wait function,
336  * allowing us to determine whether the call came from an MGETHDR or
337  * an MGET.
338  */
339 #define MGETHDR_C      1
340 #define MGET_C         2
341
342 /*
343  * mbuf allocation/deallocation macros (YYY deprecated, too big):
344  *
345  *      MGET(struct mbuf *m, int how, int type)
346  * allocates an mbuf and initializes it to contain internal data.
347  *
348  *      MGETHDR(struct mbuf *m, int how, int type)
349  * allocates an mbuf and initializes it to contain a packet header
350  * and internal data.
351  */
352 #define MGET(m, how, type) do {                                         \
353         (m) = m_get((how), (type));                                     \
354 } while (0)
355
356 #define MGETHDR(m, how, type) do {                                      \
357         (m) = m_gethdr((how), (type));                                  \
358 } while (0)
359
360 /*
361  * MCLGET adds such clusters to a normal mbuf.  The flag M_EXT is set upon
362  * success.
363  * Deprecated.  Use m_getcl() or m_getl() instead.
364  */
365 #define MCLGET(m, how) do {                                             \
366         m_mclget((m), (how));                                           \
367 } while (0)
368
369 /*
370  * NB: M_COPY_PKTHDR is deprecated; use either M_MOVE_PKTHDR
371  *     or m_dup_pkthdr.
372  */
373 /*
374  * Move mbuf pkthdr from "from" to "to".
375  * from should have M_PKTHDR set, and to must be empty.
376  * from no longer has a pkthdr after this operation.
377  */
378 #define M_MOVE_PKTHDR(_to, _from)       m_move_pkthdr((_to), (_from))
379
380 /*
381  * Set the m_data pointer of a newly-allocated mbuf (m_get/MGET) to place
382  * an object of the specified size at the end of the mbuf, longword aligned.
383  */
384 #define M_ALIGN(m, len) do {                                            \
385         (m)->m_data += (MLEN - (len)) & ~(sizeof(long) - 1);            \
386 } while (0)
387
388 /*
389  * As above, for mbufs allocated with m_gethdr/MGETHDR
390  * or initialized by M_COPY_PKTHDR.
391  */
392 #define MH_ALIGN(m, len) do {                                           \
393         (m)->m_data += (MHLEN - (len)) & ~(sizeof(long) - 1);           \
394 } while (0)
395
396 /*
397  * Check if we can write to an mbuf.
398  */
399 #define M_EXT_WRITABLE(m)       (m_sharecount(m) == 1)
400 #define M_WRITABLE(m)           (!((m)->m_flags & M_EXT) || M_EXT_WRITABLE(m))
401
402 /*
403  * Check if the supplied mbuf has a packet header, or else panic.
404  */
405 #define M_ASSERTPKTHDR(m)                                               \
406         KASSERT(m != NULL && m->m_flags & M_PKTHDR,                     \
407                 ("%s: invalid mbuf or no mbuf packet header!", __func__))
408
409 /*
410  * Compute the amount of space available before the current start of data.
411  * The M_EXT_WRITABLE() is a temporary, conservative safety measure: the burden
412  * of checking writability of the mbuf data area rests solely with the caller.
413  */
414 #define M_LEADINGSPACE(m)                                               \
415         ((m)->m_flags & M_EXT ?                                         \
416             (M_EXT_WRITABLE(m) ? (m)->m_data - (m)->m_ext.ext_buf : 0): \
417             (m)->m_flags & M_PKTHDR ? (m)->m_data - (m)->m_pktdat :     \
418             (m)->m_data - (m)->m_dat)
419
420 /*
421  * Compute the amount of space available after the end of data in an mbuf.
422  * The M_WRITABLE() is a temporary, conservative safety measure: the burden
423  * of checking writability of the mbuf data area rests solely with the caller.
424  */
425 #define M_TRAILINGSPACE(m)                                              \
426         ((m)->m_flags & M_EXT ?                                         \
427             (M_WRITABLE(m) ? (m)->m_ext.ext_buf + (m)->m_ext.ext_size   \
428                 - ((m)->m_data + (m)->m_len) : 0) :                     \
429             &(m)->m_dat[MLEN] - ((m)->m_data + (m)->m_len))
430
431 /*
432  * Arrange to prepend space of size plen to mbuf m.
433  * If a new mbuf must be allocated, how specifies whether to wait.
434  * If how is MB_DONTWAIT and allocation fails, the original mbuf chain
435  * is freed and m is set to NULL.
436  */
437 #define M_PREPEND(m, plen, how) do {                                    \
438         struct mbuf **_mmp = &(m);                                      \
439         struct mbuf *_mm = *_mmp;                                       \
440         int _mplen = (plen);                                            \
441         int __mhow = (how);                                             \
442                                                                         \
443         if (M_LEADINGSPACE(_mm) >= _mplen) {                            \
444                 _mm->m_data -= _mplen;                                  \
445                 _mm->m_len += _mplen;                                   \
446         } else                                                          \
447                 _mm = m_prepend(_mm, _mplen, __mhow);                   \
448         if (_mm != NULL && (_mm->m_flags & M_PKTHDR))                   \
449                 _mm->m_pkthdr.len += _mplen;                            \
450         *_mmp = _mm;                                                    \
451 } while (0)
452
453 /* Length to m_copy to copy all. */
454 #define M_COPYALL       1000000000
455
456 /* Compatibility with 4.3 */
457 #define m_copy(m, o, l) m_copym((m), (o), (l), MB_DONTWAIT)
458
459 #ifdef _KERNEL
460 extern  u_int            m_clalloc_wid; /* mbuf cluster wait count */
461 extern  u_int            m_mballoc_wid; /* mbuf wait count */
462 extern  int              max_linkhdr;   /* largest link-level header */
463 extern  int              max_protohdr;  /* largest protocol header */
464 extern  int              max_hdr;       /* largest link+protocol header */
465 extern  int              max_datalen;   /* MHLEN - max_hdr */
466 extern  int              mbuf_wait;     /* mbuf sleep time */
467 extern  int              nmbclusters;
468 extern  int              nmbufs;
469
470 struct uio;
471
472 void             m_adj(struct mbuf *, int);
473 void             m_align(struct mbuf *, int);
474 int              m_apply(struct mbuf *, int, int,
475                     int (*)(void *, void *, u_int), void *);
476 int             m_append(struct mbuf *, int, c_caddr_t);
477 void             m_cat(struct mbuf *, struct mbuf *);
478 u_int            m_countm(struct mbuf *m, struct mbuf **lastm, u_int *mbcnt);
479 void             m_copyback(struct mbuf *, int, int, caddr_t);
480 void             m_copydata(const struct mbuf *, int, int, caddr_t);
481 struct  mbuf    *m_copym(const struct mbuf *, int, int, int);
482 struct  mbuf    *m_copypacket(struct mbuf *, int);
483 struct  mbuf    *m_defrag(struct mbuf *, int);
484 struct  mbuf    *m_defrag_nofree(struct mbuf *, int);
485 struct  mbuf    *m_devget(char *, int, int, struct ifnet *,
486                   void (*copy)(volatile const void *, volatile void *, size_t));
487 struct  mbuf    *m_dup(struct mbuf *, int);
488 struct  mbuf    *m_dup_data(struct mbuf *, int);
489 int              m_dup_pkthdr(struct mbuf *, const struct mbuf *, int);
490 #ifdef MBUF_DEBUG
491 struct  mbuf    *_m_free(struct mbuf *, const char *name);
492 void             _m_freem(struct mbuf *, const char *name);
493 #else
494 struct  mbuf    *m_free(struct mbuf *);
495 void             m_freem(struct mbuf *);
496 #endif
497 struct  mbuf    *m_get(int, int);
498 struct  mbuf    *m_getc(int len, int how, int type);
499 struct  mbuf    *m_getcl(int how, short type, int flags);
500 struct  mbuf    *m_getclr(int, int);
501 struct  mbuf    *m_gethdr(int, int);
502 struct  mbuf    *m_getm(struct mbuf *, int, int, int);
503 struct  mbuf    *m_getptr(struct mbuf *, int, int *);
504 struct  mbuf    *m_last(struct mbuf *m);
505 u_int            m_lengthm(struct mbuf *m, struct mbuf **lastm);
506 void             m_move_pkthdr(struct mbuf *, struct mbuf *);
507 struct  mbuf    *m_prepend(struct mbuf *, int, int);
508 void             m_print(const struct mbuf *m);
509 struct  mbuf    *m_pulldown(struct mbuf *, int, int, int *);
510 struct  mbuf    *m_pullup(struct mbuf *, int);
511 struct  mbuf    *m_split(struct mbuf *, int, int);
512 struct  mbuf    *m_uiomove(struct uio *);
513 void            m_mclget(struct mbuf *m, int how);
514 int             m_sharecount(struct mbuf *m);
515 void            m_chtype(struct mbuf *m, int type);
516 int             m_devpad(struct mbuf *m, int padto);
517
518 #ifdef MBUF_DEBUG
519
520 void            mbuftrackid(struct mbuf *, int);
521
522 #define m_free(m)       _m_free(m, __func__)
523 #define m_freem(m)      _m_freem(m, __func__)
524
525 #else
526
527 #define mbuftrackid(m, id)      /* empty */
528
529 #endif
530
531 /*
532  * Allocate the right type of mbuf for the desired total length.
533  * The mbuf returned does not necessarily cover the entire requested length.
534  * This function follows mbuf chaining policy of allowing MINCLSIZE
535  * amount of chained mbufs.
536  */
537 static __inline struct mbuf *
538 m_getl(int len, int how, int type, int flags, int *psize)
539 {
540         struct mbuf *m;
541         int size;
542
543         if (len >= MINCLSIZE) {
544                 m = m_getcl(how, type, flags);
545                 size = MCLBYTES;
546         } else if (flags & M_PKTHDR) {
547                 m = m_gethdr(how, type);
548                 size = MHLEN;
549         } else {
550                 m = m_get(how, type);
551                 size = MLEN;
552         }
553         if (psize != NULL)
554                 *psize = size;
555         return (m);
556 }
557
558 /*
559  * Get a single mbuf that covers the requested number of bytes.
560  * This function does not create mbuf chains.  It explicitly marks
561  * places in the code that abuse mbufs for contiguous data buffers.
562  */
563 static __inline struct mbuf *
564 m_getb(int len, int how, int type, int flags)
565 {
566         struct mbuf *m;
567         int mbufsize = (flags & M_PKTHDR) ? MHLEN : MLEN;
568
569         if (len > mbufsize)
570                 m = m_getcl(how, type, flags);
571         else if (flags & M_PKTHDR)
572                 m = m_gethdr(how, type);
573         else
574                 m = m_get(how, type);
575         return (m);
576 }
577
578 /*
579  * Packets may have annotations attached by affixing a list
580  * of "packet tags" to the pkthdr structure.  Packet tags are
581  * dynamically allocated semi-opaque data structures that have
582  * a fixed header (struct m_tag) that specifies the size of the
583  * memory block and a <cookie,type> pair that identifies it.
584  * The cookie is a 32-bit unique unsigned value used to identify
585  * a module or ABI.  By convention this value is chose as the
586  * date+time that the module is created, expressed as the number of
587  * seconds since the epoch (e.g. using date -u +'%s').  The type value
588  * is an ABI/module-specific value that identifies a particular annotation
589  * and is private to the module.  For compatibility with systems
590  * like openbsd that define packet tags w/o an ABI/module cookie,
591  * the value PACKET_ABI_COMPAT is used to implement m_tag_get and
592  * m_tag_find compatibility shim functions and several tag types are
593  * defined below.  Users that do not require compatibility should use
594  * a private cookie value so that packet tag-related definitions
595  * can be maintained privately.
596  *
597  * Note that the packet tag returned by m_tag_alloc has the default
598  * memory alignment implemented by kmalloc.  To reference private data
599  * one can use a construct like:
600  *
601  *      struct m_tag *mtag = m_tag_alloc(...);
602  *      struct foo *p = m_tag_data(mtag);
603  *
604  * if the alignment of struct m_tag is sufficient for referencing members
605  * of struct foo.  Otherwise it is necessary to embed struct m_tag within
606  * the private data structure to insure proper alignment; e.g.
607  *
608  *      struct foo {
609  *              struct m_tag    tag;
610  *              ...
611  *      };
612  *      struct foo *p = (struct foo *)m_tag_alloc(...);
613  *      struct m_tag *mtag = &p->tag;
614  */
615
616 #define PACKET_TAG_NONE                         0  /* Nadda */
617
618 /* Packet tag for use with PACKET_ABI_COMPAT */
619 #define PACKET_TAG_IPSEC_IN_DONE                1  /* IPsec applied, in */
620 /* struct tdb_indent */
621 #define PACKET_TAG_IPSEC_OUT_DONE               2  /* IPsec applied, out */
622 /* struct tdb_indent */
623 #define PACKET_TAG_IPSEC_IN_CRYPTO_DONE         3  /* NIC IPsec crypto done */
624 /* struct tdb_indent, never added */
625 #define PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED      4  /* NIC IPsec crypto req'ed */
626 /* struct tdb_indent, never added */
627 #define PACKET_TAG_IPSEC_PENDING_TDB            5  /* Reminder to do IPsec */
628 /* struct tdb_indent, never added */
629 #define PACKET_TAG_ENCAP                        6 /* Encap.  processing */
630 /* struct ifnet *, the GIF interface */
631 #define PACKET_TAG_IPSEC_HISTORY                7 /* IPSEC history */
632 /* struct ipsec_history */
633 #define PACKET_TAG_IPV6_INPUT                   8 /* IPV6 input processing */
634 /* struct ip6aux */
635 #define PACKET_TAG_IPFW_DIVERT                  9 /* divert info */
636 /* struct divert_info */
637 #define PACKET_TAG_DUMMYNET                     15 /* dummynet info */
638 /* struct dn_pkt */
639 #define PACKET_TAG_IPFORWARD                    18 /* ipforward info */
640 /* struct sockaddr_in */
641 #define PACKET_TAG_IPSRCRT                      27 /* IP srcrt opts */
642 /* struct ip_srcrt_opt */
643 #define PACKET_TAG_CARP                         28 /* CARP info */
644 /* struct pf_mtag */
645 #define PACKET_TAG_PF                           29 /* PF info */
646
647 #define PACKET_TAG_PF_DIVERT                    0x0200 /* pf(4) diverted packet */
648         
649
650 /* Packet tag routines */
651 struct  m_tag   *m_tag_alloc(u_int32_t, int, int, int);
652 void             m_tag_free(struct m_tag *);
653 void             m_tag_prepend(struct mbuf *, struct m_tag *);
654 void             m_tag_unlink(struct mbuf *, struct m_tag *);
655 void             m_tag_delete(struct mbuf *, struct m_tag *);
656 void             m_tag_delete_chain(struct mbuf *);
657 struct  m_tag   *m_tag_locate(struct mbuf *, u_int32_t, int, struct m_tag *);
658 struct  m_tag   *m_tag_copy(struct m_tag *, int);
659 int              m_tag_copy_chain(struct mbuf *, const struct mbuf *, int);
660 void             m_tag_init(struct mbuf *);
661 struct  m_tag   *m_tag_first(struct mbuf *);
662 struct  m_tag   *m_tag_next(struct mbuf *, struct m_tag *);
663
664 /* these are for openbsd compatibility */
665 #define MTAG_ABI_COMPAT         0               /* compatibility ABI */
666
667 static __inline void *
668 m_tag_data(struct m_tag *tag)
669 {
670         return ((void *)(tag + 1));
671 }
672
673 static __inline struct m_tag *
674 m_tag_get(int type, int length, int wait)
675 {
676         return m_tag_alloc(MTAG_ABI_COMPAT, type, length, wait);
677 }
678
679 static __inline struct m_tag *
680 m_tag_find(struct mbuf *m, int type, struct m_tag *start)
681 {
682         return m_tag_locate(m, MTAG_ABI_COMPAT, type, start);
683 }
684
685 #endif  /* _KERNEL */
686
687 #endif  /* _KERNEL || _KERNEL_STRUCTURES */
688 #endif  /* !_SYS_MBUF_H_ */