• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2013-2019. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote
13  *    products derived from this software without specific prior
14  *    written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Description: declaration for socket filter
29  * Author: none
30  * Create: 2013
31  */
32 
33 #ifndef __LWIP_FILTER_H__
34 #define __LWIP_FILTER_H__
35 
36 #include "lwip/opt.h"
37 
38 #if LWIP_SOCK_FILTER
39 
40 #include "lwip/pbuf.h"
41 #include "lwip/mem.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif /*__cplusplus */
46 
47 /* @brief Defines codes of socket filter. */
48 struct sock_filter {
49   u16_t code; /* compiler code */
50   u8_t jt;    /* jump if true */
51   u8_t jf;    /* jump if false */
52   u32_t k;    /* misc field */
53 };
54 
55 /* @brief Defines value types of SO_ATTACH_FILTER for setsockopt. */
56 struct sock_fprog {
57   u16_t len;                    /* the length of the filter array */
58   struct sock_filter *filter;   /* pointer of socket filter codes */
59 };
60 
61 #define LSF_CLASS(code) ((code) & 0x07)
62 #define         LSF_LD          0x00
63 #define         LSF_LDX         0x01
64 #define         LSF_ST          0x02
65 #define         LSF_STX         0x03
66 #define         LSF_ALU         0x04
67 #define         LSF_JMP         0x05
68 #define         LSF_RET         0x06
69 #define         LSF_MISC        0x07
70 
71 /* ld/ldx */
72 #define LSF_SIZE(code)  ((code) & 0x18)
73 #define         LSF_W           0x00
74 #define         LSF_H           0x08
75 #define         LSF_B           0x10
76 #define LSF_MODE(code)  ((code) & 0xe0)
77 #define         LSF_IMM         0x00
78 #define         LSF_ABS         0x20
79 #define         LSF_IND         0x40
80 #define         LSF_MEM         0x60
81 #define         LSF_LEN         0x80
82 #define         LSF_MSH         0xa0  /* unsupported */
83 
84 /* alu/jmp */
85 #define LSF_OP(code)    ((code) & 0xf0)
86 #define         LSF_ADD         0x00
87 #define         LSF_SUB         0x10
88 #define         LSF_MUL         0x20
89 #define         LSF_DIV         0x30
90 #define         LSF_OR          0x40
91 #define         LSF_AND         0x50
92 #define         LSF_LSH         0x60
93 #define         LSF_RSH         0x70
94 #define         LSF_NEG         0x80
95 #define         LSF_JA          0x00
96 #define         LSF_JEQ         0x10
97 #define         LSF_JGT         0x20
98 #define         LSF_JGE         0x30
99 #define         LSF_JSET        0x40
100 #define LSF_SRC(code)   ((code) & 0x08)
101 #define         LSF_K           0x00
102 #define         LSF_X           0x08
103 
104 /* ret - LSF_K and LSF_X also apply */
105 #define LSF_RVAL(code)  ((code) & 0x18)
106 #define         LSF_A           0x10
107 
108 /* misc */
109 #define LSF_MISCOP(code) ((code) & 0xf8)
110 #define         LSF_TAX         0x00
111 #define         LSF_TXA         0x80
112 
113 #ifndef LSF_MAXINSNS
114 #define LSF_MAXINSNS 128
115 #endif
116 
117 
118 /*
119  * Number of scratch memory words for: LSF_ST and LSF_STX
120  */
121 #define LSF_MEMWORDS 16
122 
123 struct netconn;
124 s32_t sock_filter(struct netconn *conn, struct pbuf *pbuf);
125 u32_t sock_run_filter(struct pbuf *pbuf, struct sock_filter *filter, u16_t flen);
126 s32_t sock_attach_filter(struct sock_fprog *fprog, struct netconn *conn);
127 s32_t sock_detach_filter(struct netconn *conn);
128 s32_t sock_check_filter(struct sock_filter *filter, int len);
129 
130 #ifdef __cplusplus
131 }
132 #endif /* __cplusplus */
133 
134 #endif /* LWIP_SOCK_FILTER */
135 #endif /* __LWIP_FILTER_H__ */
136 
137