1 /*
2 ** Copyright (C) 2007-2009 Sourcefire, Inc.
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License Version 2 as
6 ** published by the Free Software Foundation. You may not use, modify or
7 ** distribute this program under any other version of the GNU General
8 ** Public License.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef IPV6_PORT_H
21 #define IPV6_PORT_H
22
23 #include "sf_types.h"
24 #include "debug.h"
25
26 ///////////////////
27 /* IPv6 and IPv4 */
28 #ifdef SUP_IP6
29
30 #include "sf_ip.h"
31
32 typedef sfip_t snort_ip;
33 typedef sfip_t *snort_ip_p;
34
35 #define IpAddrNode sfip_node_t
36 #define IpAddrSet sfip_var_t
37 #define IpAddrSetContains(x,y) sfvar_ip_in(x, y)
38 #define IpAddrSetPrint sfvar_print
39
40 #ifdef inet_ntoa
41 #undef inet_ntoa
42 #endif
43 #define inet_ntoa sfip_ntoa
44
45 #define GET_SRC_IP(p) (p->iph_api->iph_ret_src(p))
46 #define GET_DST_IP(p) (p->iph_api->iph_ret_dst(p))
47
48 #define GET_ORIG_SRC(p) (p->orig_iph_api->orig_iph_ret_src(p))
49 #define GET_ORIG_DST(p) (p->orig_iph_api->orig_iph_ret_dst(p))
50
51 /* These are here for backwards compatibility */
52 #define GET_SRC_ADDR(x) GET_SRC_IP(x)
53 #define GET_DST_ADDR(x) GET_DST_IP(x)
54
55 #define IP_EQUALITY(x,y) (sfip_compare(x,y) == SFIP_EQUAL)
56 #define IP_EQUALITY_UNSET(x,y) (sfip_compare_unset(x,y) == SFIP_EQUAL)
57 #define IP_LESSER(x,y) (sfip_compare(x,y) == SFIP_LESSER)
58 #define IP_GREATER(x,y) (sfip_compare(x,y) == SFIP_GREATER)
59
60 #define GET_IPH_TOS(p) p->iph_api->iph_ret_tos(p)
61 #define GET_IPH_LEN(p) p->iph_api->iph_ret_len(p)
62 #define GET_IPH_TTL(p) p->iph_api->iph_ret_ttl(p)
63 #define GET_IPH_ID(p) p->iph_api->iph_ret_id(p)
64 #define GET_IPH_OFF(p) p->iph_api->iph_ret_off(p)
65 #define GET_IPH_VER(p) p->iph_api->iph_ret_ver(p)
66 #define GET_IPH_PROTO(p) p->iph_api->iph_ret_proto(p)
67
68 #define GET_ORIG_IPH_PROTO(p) p->orig_iph_api->orig_iph_ret_proto(p)
69 #define GET_ORIG_IPH_VER(p) p->orig_iph_api->orig_iph_ret_ver(p)
70 #define GET_ORIG_IPH_LEN(p) p->orig_iph_api->orig_iph_ret_len(p)
71 #define GET_ORIG_IPH_OFF(p) p->orig_iph_api->orig_iph_ret_off(p)
72 #define GET_ORIG_IPH_PROTO(p) p->orig_iph_api->orig_iph_ret_proto(p)
73
74 #define IS_IP4(x) (x->family == AF_INET)
75 #define IS_IP6(x) (x->family == AF_INET6)
76 /* XXX make sure these aren't getting confused with sfip_is_valid within the code */
77 #define IPH_IS_VALID(p) iph_is_valid(p)
78
79 #define IP_CLEAR(x) x.bits = x.family = x.ip32[0] = x.ip32[1] = x.ip32[2] = x.ip32[3] = 0;
80
81 #define IS_SET(x) sfip_is_set(&x)
82
83 /* This loop trickery is intentional. If each copy is performed
84 * individually on each field, then the following expression gets broken:
85 *
86 * if(conditional) IP_COPY_VALUE(a,b);
87 *
88 * If the macro is instead enclosed in braces, then having a semicolon
89 * trailing the macro causes compile breakage.
90 * So: use loop. */
91 #define IP_COPY_VALUE(x,y) \
92 do { \
93 x.bits = y->bits; \
94 x.family = y->family; \
95 x.ip32[0] = y->ip32[0]; \
96 x.ip32[1] = y->ip32[1]; \
97 x.ip32[2] = y->ip32[2]; \
98 x.ip32[3] = y->ip32[3]; \
99 } while(0)
100
101 #define GET_IPH_HLEN(p) (p->iph_api->iph_ret_hlen(p))
102 #define SET_IPH_HLEN(p, val)
103
104 #define GET_IP_DGMLEN(p) IS_IP6(p) ? (ntohs(GET_IPH_LEN(p)) + (GET_IPH_HLEN(p) << 2)) : ntohs(GET_IPH_LEN(p))
105 #define GET_IP_PAYLEN(p) IS_IP6(p) ? ntohs(GET_IPH_LEN(p)) : (ntohs(GET_IPH_LEN(p)) - (GET_IPH_HLEN(p) << 2))
106
107 #define IP_ARG(ipt) (&ipt)
108 #define IP_PTR(ipp) (ipp)
109 #define IP_VAL(ipt) (*ipt)
110 #define IP_SIZE(ipp) (sfip_size(ipp))
111
sfip_equal(snort_ip * ip1,snort_ip * ip2)112 static INLINE int sfip_equal (snort_ip* ip1, snort_ip* ip2)
113 {
114 if ( ip1->family != ip2->family )
115 {
116 return 0;
117 }
118 if ( ip1->family == AF_INET )
119 {
120 return _ip4_cmp(ip1->ip32[0], ip2->ip32[0]) == SFIP_EQUAL;
121 }
122 if ( ip1->family == AF_INET6 )
123 {
124 return _ip6_cmp(ip1, ip2) == SFIP_EQUAL;
125 }
126 return 0;
127 }
128
129 #else
130 ///////////////
131 /* IPv4 only */
132 #include <sys/types.h>
133 #ifdef HAVE_CONFIG_H
134 #include <config.h>
135 #endif
136
137 typedef u_int32_t snort_ip; /* 32 bits only -- don't use unsigned long */
138 typedef u_int32_t snort_ip_p; /* 32 bits only -- don't use unsigned long */
139
140 #define IP_SRC_EQUALITY(x,y) (x->ip_addr == (y->iph->ip_src.s_addr & x->netmask))
141 #define IP_DST_EQUALITY(x,y) (x->ip_addr == (y->iph->ip_dst.s_addr & x->netmask))
142
143 #define GET_SRC_IP(x) x->iph->ip_src.s_addr
144 #define GET_DST_IP(x) x->iph->ip_dst.s_addr
145
146 #define GET_ORIG_SRC(p) (p->orig_iph->ip_src.s_addr)
147 #define GET_ORIG_DST(p) (p->orig_iph->ip_dst.s_addr)
148
149 #define GET_SRC_ADDR(x) x->iph->ip_src
150 #define GET_DST_ADDR(x) x->iph->ip_dst
151
152 #define IP_CLEAR_SRC(x) x->iph->ip_src.s_addr = 0
153 #define IP_CLEAR_DST(x) x->iph->ip_dst.s_addr = 0
154
155 #define IP_EQUALITY(x,y) (x == y)
156 #define IP_EQUALITY_UNSET(x,y) (x == y)
157 #define IP_LESSER(x,y) (x < y)
158 #define IP_GREATER(x,y) (x > y)
159
160 #define GET_IPH_PROTO(p) p->iph->ip_proto
161 #define GET_IPH_TOS(p) p->iph->ip_tos
162 #define GET_IPH_LEN(p) p->iph->ip_len
163 #define GET_IPH_TTL(p) p->iph->ip_ttl
164 #define GET_IPH_VER(p) ((p->iph->ip_verhl & 0xf0) >> 4)
165 #define GET_IPH_ID(p) p->iph->ip_id
166 #define GET_IPH_OFF(p) p->iph->ip_off
167
168 #define GET_ORIG_IPH_VER(p) IP_VER(p->orig_iph)
169 #define GET_ORIG_IPH_LEN(p) p->orig_iph->ip_len
170 #define GET_ORIG_IPH_OFF(p) p->orig_iph->ip_off
171 #define GET_ORIG_IPH_PROTO(p) p->orig_iph->ip_proto
172
173 #define IS_IP4(x) 1
174 #define IS_IP6(x) 0
175 #define IPH_IS_VALID(p) p->iph
176
177 #define IP_CLEAR(x) x = 0;
178 #define IS_SET(x) x
179
180 #define IP_COPY_VALUE(x,y) x = y
181
182 #define GET_IPH_HLEN(p) ((p)->iph->ip_verhl & 0x0f)
183 #define SET_IPH_HLEN(p, val) (((IPHdr *)(p)->iph)->ip_verhl = (unsigned char)(((p)->iph->ip_verhl & 0xf0) | ((val) & 0x0f)))
184
185 #define GET_IP_DGMLEN(p) ntohs(GET_IPH_LEN(p))
186 #define GET_IP_PAYLEN(p) ntohs(GET_IPH_LEN(p)) - (GET_IPH_HLEN(p) << 2)
187
188 #define IP_ARG(ipt) (ipt)
189 #define IP_PTR(ipp) (&ipp)
190 #define IP_VAL(ipt) (ipt)
191 #define IP_SIZE(ipp) (sizeof(ipp))
192
sfip_equal(snort_ip ip1,snort_ip ip2)193 static INLINE int sfip_equal (snort_ip ip1, snort_ip ip2)
194 {
195 return IP_EQUALITY(ip1, ip2);
196 }
197
198 #endif /* SUP_IP6 */
199
200 #if !defined(IPPROTO_IPIP) && defined(WIN32) /* Needed for some Win32 */
201 #define IPPROTO_IPIP 4
202 #endif
203
204 #endif /* IPV6_PORT_H */
205