1 /*
2 ** Copyright (C) 2014-2021 Cisco and/or its affiliates. All rights reserved.
3 ** Copyright (C) 2007-2013 Sourcefire, Inc.
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License Version 2 as
7 ** published by the Free Software Foundation.  You may not use, modify or
8 ** distribute this program under any other version of the GNU General
9 ** Public License.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20 
21 #ifndef IPV6_PORT_H
22 #define IPV6_PORT_H
23 
24 #include "snort_debug.h"
25 
26 /*****************/
27 /* IPv6 and IPv4 */
28 
29 #include "sf_ip.h"
30 
31 #define IpAddrNode sfip_node_t
32 #define IpAddrSet sfip_var_t
33 #define IpAddrSetContains(x,y) sfvar_ip_in(x, y)
34 #define IpAddrSetPrint sfvar_print
35 
36 #ifdef inet_ntoa
37 #undef inet_ntoa
38 #endif
39 #define inet_ntoa sfip_ntoa
40 
41 #define GET_SRC_IP(p) ((p)->iph_api->iph_ret_src(p))
42 #define GET_DST_IP(p) ((p)->iph_api->iph_ret_dst(p))
43 
44 #define GET_ORIG_SRC(p) ((p)->orig_iph_api->orig_iph_ret_src(p))
45 #define GET_ORIG_DST(p) ((p)->orig_iph_api->orig_iph_ret_dst(p))
46 
47 /* These are here for backwards compatibility */
48 #define GET_SRC_ADDR(x) GET_SRC_IP(x)
49 #define GET_DST_ADDR(x) GET_DST_IP(x)
50 
51 #define IP_EQUALITY(x,y) (sfip_compare((x),(y)) == SFIP_EQUAL)
52 #define IP_EQUALITY_UNSET(x,y) (sfip_compare_unset((x),(y)) == SFIP_EQUAL)
53 #define IP_LESSER(x,y)   (sfip_compare((x),(y)) == SFIP_LESSER)
54 #define IP_GREATER(x,y)  (sfip_compare((x),(y)) == SFIP_GREATER)
55 
56 #define IS_IP4(x) ((x)->family == AF_INET)
57 #define IS_IP6(x) ((x)->family == AF_INET6)
58 
59 #define IS_OUTER_IP4(x) ((x)->outer_family == AF_INET)
60 #define IS_OUTER_IP6(x) ((x)->outer_family == AF_INET6)
61 
62 #define GET_IPH_TOS(p)   (p)->iph_api->iph_ret_tos(p)
63 #define GET_IPH_LEN(p)   (p)->iph_api->iph_ret_len(p)
64 #define GET_IPH_TTL(p)   (p)->iph_api->iph_ret_ttl(p)
65 #define GET_IPH_ID(p)    (p)->iph_api->iph_ret_id(p)
66 #define GET_IPH_OFF(p)   (p)->iph_api->iph_ret_off(p)
67 #define GET_IPH_VER(p)   (p)->iph_api->iph_ret_ver(p)
68 #define GET_IPH_PROTO(p) ((uint8_t)(IS_IP6(p) ? ((p)->ip6h->next) : ((p)->iph_api->iph_ret_proto(p))))
69 
70 #define GET_ORIG_IPH_PROTO(p)   (p)->orig_iph_api->orig_iph_ret_proto(p)
71 #define GET_ORIG_IPH_VER(p)     (p)->orig_iph_api->orig_iph_ret_ver(p)
72 #define GET_ORIG_IPH_LEN(p)     (p)->orig_iph_api->orig_iph_ret_len(p)
73 #define GET_ORIG_IPH_OFF(p)     (p)->orig_iph_api->orig_iph_ret_off(p)
74 
75 /* XXX make sure these aren't getting confused with sfip_is_valid within the code */
76 #define IPH_IS_VALID(p) iph_is_valid(p)
77 
78 #define IP_CLEAR(x) (x).family = (x).ia32[0] = (x).ia32[1] = (x).ia32[2] = (x).ia32[3] = 0;
79 
80 #define IP_IS_SET(x) sfip_is_set(&x)
81 
82 /* This loop trickery is intentional.  If each copy is performed
83  * individually on each field, then the following expression gets broken:
84  *
85  *      if(conditional) IP_COPY_VALUE(a,b);
86  *
87  * If the macro is instead enclosed in braces, then having a semicolon
88  * trailing the macro causes compile breakage.
89  * So: use loop. */
90 #define IP_COPY_VALUE(dst, src)  sfip_set_ip(&(dst), src)
91 
92 #define GET_IPH_HLEN(p) ((p)->iph_api->iph_ret_hlen(p))
93 #define SET_IPH_HLEN(p, val)
94 
95 #define GET_IP_DGMLEN(p) IS_IP6(p) ? (ntohs(GET_IPH_LEN(p)) + (GET_IPH_HLEN(p) << 2)) : ntohs(GET_IPH_LEN(p))
96 #define GET_IP_PAYLEN(p) IS_IP6(p) ? ntohs(GET_IPH_LEN(p)) : (ntohs(GET_IPH_LEN(p)) - (GET_IPH_HLEN(p) << 2))
97 
98 #define IP_ARG(ipt)  (&ipt)
99 #define IP_PTR(ipp)  (ipp)
100 #define IP_VAL(ipt)  (*ipt)
101 
102 #define GET_INNER_SRC_IP(p)  (IS_IP6(p) ? (&((p)->inner_ip6h.ip_addrs->ip_src)):(&((p)->inner_ip4h.ip_addrs->ip_src)))
103 #define GET_INNER_DST_IP(p)  (IS_IP6(p) ? (&((p)->inner_ip6h.ip_addrs->ip_dst)):(&((p)->inner_ip4h.ip_addrs->ip_dst)))
104 #define GET_OUTER_SRC_IP(p)  (IS_OUTER_IP6(p) ? (&((p)->outer_ip6h.ip_addrs->ip_src)):(&((p)->outer_ip4h.ip_addrs->ip_src)))
105 #define GET_OUTER_DST_IP(p)  (IS_OUTER_IP6(p) ? (&((p)->outer_ip6h.ip_addrs->ip_dst)):(&((p)->outer_ip4h.ip_addrs->ip_dst)))
106 #if 0
107 static inline int sfip_equal (sfaddr_t* ip1, sfaddr_t* ip2)
108 {
109     return _ip6_cmp(ip1, ip2) == SFIP_EQUAL;
110 }
111 #endif
112 
113 
114 #if !defined(IPPROTO_IPIP) && defined(WIN32)  /* Needed for some Win32 */
115 #define IPPROTO_IPIP 4
116 #endif
117 
118 #endif /* IPV6_PORT_H */
119