suricata
detect-engine-address-ipv6.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * IPV6 Address part of the detection engine.
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "decode.h"
29 #include "detect.h"
30 #include "flow-var.h"
31 
32 #include "util-cidr.h"
33 #include "util-unittest.h"
34 
35 #include "detect-engine-address.h"
37 #include "detect-engine-siggroup.h"
38 #include "detect-engine-port.h"
39 
40 #include "util-debug.h"
41 
42 /**
43  * \brief Compares 2 ipv6 addresses and returns if the first address(a) is less
44  * than the second address(b) or not.
45  *
46  * \param a The first ipv6 address to be compared.
47  * \param b The second ipv6 address to be compared.
48  *
49  * \retval 1 If a < b.
50  * \retval 0 Otherwise, i.e. a >= b.
51  */
53 {
54  int i = 0;
55 
56  for (i = 0; i < 4; i++) {
57  if (SCNtohl(a->addr_data32[i]) < SCNtohl(b->addr_data32[i]))
58  return 1;
59  if (SCNtohl(a->addr_data32[i]) > SCNtohl(b->addr_data32[i]))
60  break;
61  }
62 
63  return 0;
64 }
65 
66 int AddressIPv6LtU32(uint32_t *a, uint32_t *b)
67 {
68  int i = 0;
69 
70  for (i = 0; i < 4; i++) {
71  if (SCNtohl(a[i]) < SCNtohl(b[i]))
72  return 1;
73  if (SCNtohl(a[i]) > SCNtohl(b[i]))
74  break;
75  }
76 
77  return 0;
78 }
79 
80 /**
81  * \brief Compares 2 ipv6 addresses and returns if the first address(a) is
82  * greater than the second address(b) or not.
83  *
84  * \param a The first ipv6 address to be compared.
85  * \param b The second ipv6 address to be compared.
86  *
87  * \retval 1 If a > b.
88  * \retval 0 Otherwise, i.e. a <= b.
89  */
91 {
92  int i = 0;
93 
94  for (i = 0; i < 4; i++) {
95  if (SCNtohl(a->addr_data32[i]) > SCNtohl(b->addr_data32[i]))
96  return 1;
97  if (SCNtohl(a->addr_data32[i]) < SCNtohl(b->addr_data32[i]))
98  break;
99  }
100 
101  return 0;
102 }
103 
104 int AddressIPv6GtU32(uint32_t *a, uint32_t *b)
105 {
106  int i = 0;
107 
108  for (i = 0; i < 4; i++) {
109  if (SCNtohl(a[i]) > SCNtohl(b[i]))
110  return 1;
111  if (SCNtohl(a[i]) < SCNtohl(b[i]))
112  break;
113  }
114 
115  return 0;
116 }
117 
118 /**
119  * \brief Compares 2 ipv6 addresses and returns if the addresses are equal
120  * or not.
121  *
122  * \param a The first ipv6 address to be compared.
123  * \param b The second ipv6 address to be compared.
124  *
125  * \retval 1 If a == b.
126  * \retval 0 Otherwise.
127  */
129 {
130  int i = 0;
131 
132  for (i = 0; i < 4; i++) {
133  if (a->addr_data32[i] != b->addr_data32[i])
134  return 0;
135  }
136 
137  return 1;
138 }
139 
140 int AddressIPv6EqU32(uint32_t *a, uint32_t *b)
141 {
142  int i = 0;
143 
144  for (i = 0; i < 4; i++) {
145  if (a[i] != b[i])
146  return 0;
147  }
148 
149  return 1;
150 }
151 
152 /**
153  * \brief Compares 2 ipv6 addresses and returns if the first address(a) is less
154  * than or equal to the second address(b) or not.
155  *
156  * \param a The first ipv6 address to be compared.
157  * \param b The second ipv6 address to be compared.
158  *
159  * \retval 1 If a <= b.
160  * \retval 0 Otherwise, i.e. a > b.
161  */
163 {
164 
165  if (AddressIPv6Eq(a, b) == 1)
166  return 1;
167  if (AddressIPv6Lt(a, b) == 1)
168  return 1;
169 
170  return 0;
171 }
172 
173 int AddressIPv6LeU32(uint32_t *a, uint32_t *b)
174 {
175 
176  if (AddressIPv6EqU32(a, b) == 1)
177  return 1;
178  if (AddressIPv6LtU32(a, b) == 1)
179  return 1;
180 
181  return 0;
182 }
183 
184 /**
185  * \brief Compares 2 ipv6 addresses and returns if the first address(a) is
186  * greater than or equal to the second address(b) or not.
187  *
188  * \param a The first ipv6 address to be compared.
189  * \param b The second ipv6 address to be compared.
190  *
191  * \retval 1 If a >= b.
192  * \retval 0 Otherwise, i.e. a < b.
193  */
195 {
196 
197  if (AddressIPv6Eq(a, b) == 1)
198  return 1;
199  if (AddressIPv6Gt(a, b) == 1)
200  return 1;
201 
202  return 0;
203 }
204 
205 int AddressIPv6GeU32(uint32_t *a, uint32_t *b)
206 {
207 
208  if (AddressIPv6EqU32(a, b) == 1)
209  return 1;
210  if (AddressIPv6GtU32(a, b) == 1)
211  return 1;
212 
213  return 0;
214 }
215 
216 /**
217  * \brief Compares 2 addresses(address ranges) and returns the relationship
218  * between the 2 addresses.
219  *
220  * \param a Pointer to the first address instance to be compared.
221  * \param b Pointer to the second address instance to be compared.
222  *
223  * \retval ADDRESS_EQ If the 2 address ranges a and b, are equal.
224  * \retval ADDRESS_ES b encapsulates a. b_ip1[...a_ip1...a_ip2...]b_ip2.
225  * \retval ADDRESS_EB a encapsulates b. a_ip1[...b_ip1....b_ip2...]a_ip2.
226  * \retval ADDRESS_LE a_ip1(...b_ip1==a_ip2...)b_ip2
227  * \retval ADDRESS_LT a_ip1(...b_ip1...a_ip2...)b_ip2
228  * \retval ADDRESS_GE b_ip1(...a_ip1==b_ip2...)a_ip2
229  * \retval ADDRESS_GT a_ip1 > b_ip2, i.e. the address range for 'a' starts only
230  * after the end of the address range for 'b'
231  */
233 {
234  if (AddressIPv6Eq(&a->ip, &b->ip) == 1 &&
235  AddressIPv6Eq(&a->ip2, &b->ip2) == 1) {
236  return ADDRESS_EQ;
237  } else if (AddressIPv6Ge(&a->ip, &b->ip) == 1 &&
238  AddressIPv6Le(&a->ip, &b->ip2) == 1 &&
239  AddressIPv6Le(&a->ip2, &b->ip2) == 1) {
240  return ADDRESS_ES;
241  } else if (AddressIPv6Le(&a->ip, &b->ip) == 1 &&
242  AddressIPv6Ge(&a->ip2, &b->ip2) == 1) {
243  return ADDRESS_EB;
244  } else if (AddressIPv6Lt(&a->ip, &b->ip) == 1 &&
245  AddressIPv6Lt(&a->ip2, &b->ip2) == 1 &&
246  AddressIPv6Ge(&a->ip2, &b->ip) == 1) {
247  return ADDRESS_LE;
248  } else if (AddressIPv6Lt(&a->ip, &b->ip) == 1 &&
249  AddressIPv6Lt(&a->ip2, &b->ip2) == 1) {
250  return ADDRESS_LT;
251  } else if (AddressIPv6Gt(&a->ip, &b->ip) == 1 &&
252  AddressIPv6Le(&a->ip, &b->ip2) == 1 &&
253  AddressIPv6Gt(&a->ip2, &b->ip2) == 1) {
254  return ADDRESS_GE;
255  } else if (AddressIPv6Gt(&a->ip, &b->ip2) == 1) {
256  return ADDRESS_GT;
257  } else {
258  /* should be unreachable */
259  SCLogDebug("Internal Error: should be unreachable\n");
260  }
261 
262  return ADDRESS_ER;
263 }
264 
265 /**
266  * \brief Takes an IPv6 address in a, and returns in b an IPv6 address which is
267  * one less than the IPv6 address in a. The address sent in a is in host
268  * order, and the address in b will be returned in network order!
269  *
270  * \param a Pointer to an IPv6 address in host order.
271  * \param b Pointer to an IPv6 address store in memory which has to be updated
272  * with the new address(a - 1).
273  */
274 static void AddressCutIPv6CopySubOne(uint32_t *a, uint32_t *b)
275 {
276  uint32_t t = a[3];
277 
278  b[0] = a[0];
279  b[1] = a[1];
280  b[2] = a[2];
281  b[3] = a[3];
282 
283  b[3]--;
284  if (b[3] > t) {
285  t = b[2];
286  b[2]--;
287  if (b[2] > t) {
288  t = b[1];
289  b[1]--;
290  if (b[1] > t)
291  b[0]--;
292  }
293  }
294 
295  b[0] = htonl(b[0]);
296  b[1] = htonl(b[1]);
297  b[2] = htonl(b[2]);
298  b[3] = htonl(b[3]);
299 
300  return;
301 }
302 
303 /**
304  * \brief Takes an IPv6 address in a, and returns in b an IPv6 address which is
305  * one more than the IPv6 address in a. The address sent in a is in host
306  * order, and the address in b will be returned in network order!
307  *
308  * \param a Pointer to an IPv6 address in host order.
309  * \param b Pointer to an IPv6 address store in memory which has to be updated
310  * with the new address(a + 1).
311  */
312 static void AddressCutIPv6CopyAddOne(uint32_t *a, uint32_t *b)
313 {
314  uint32_t t = a[3];
315 
316  b[0] = a[0];
317  b[1] = a[1];
318  b[2] = a[2];
319  b[3] = a[3];
320 
321  b[3]++;
322  if (b[3] < t) {
323  t = b[2];
324  b[2]++;
325  if (b[2] < t) {
326  t = b[1];
327  b[1]++;
328  if (b[1] < t)
329  b[0]++;
330  }
331  }
332 
333  b[0] = htonl(b[0]);
334  b[1] = htonl(b[1]);
335  b[2] = htonl(b[2]);
336  b[3] = htonl(b[3]);
337 
338  return;
339 }
340 
341 /**
342  * \brief Copies an IPv6 address in a to the b. The address in a is in host
343  * order and will be copied in network order to b!
344  *
345  * \param a Pointer to the IPv6 address to be copied.
346  * \param b Pointer to an IPv6 address in memory which will be updated with the
347  * address in a.
348  */
349 static void AddressCutIPv6Copy(uint32_t *a, uint32_t *b)
350 {
351  b[0] = htonl(a[0]);
352  b[1] = htonl(a[1]);
353  b[2] = htonl(a[2]);
354  b[3] = htonl(a[3]);
355 
356  return;
357 }
358 
361 {
362  uint32_t a_ip1[4] = { SCNtohl(a->ip.addr_data32[0]), SCNtohl(a->ip.addr_data32[1]),
363  SCNtohl(a->ip.addr_data32[2]), SCNtohl(a->ip.addr_data32[3]) };
364  uint32_t a_ip2[4] = { SCNtohl(a->ip2.addr_data32[0]), SCNtohl(a->ip2.addr_data32[1]),
365  SCNtohl(a->ip2.addr_data32[2]), SCNtohl(a->ip2.addr_data32[3]) };
366  uint32_t b_ip1[4] = { SCNtohl(b->ip.addr_data32[0]), SCNtohl(b->ip.addr_data32[1]),
367  SCNtohl(b->ip.addr_data32[2]), SCNtohl(b->ip.addr_data32[3]) };
368  uint32_t b_ip2[4] = { SCNtohl(b->ip2.addr_data32[0]), SCNtohl(b->ip2.addr_data32[1]),
369  SCNtohl(b->ip2.addr_data32[2]), SCNtohl(b->ip2.addr_data32[3]) };
370 
371  /* default to NULL */
372  *c = NULL;
373 
374  int r = DetectAddressCmpIPv6(a, b);
375  if (r != ADDRESS_ES && r != ADDRESS_EB && r != ADDRESS_LE && r != ADDRESS_GE) {
376  goto error;
377  }
378 
379  /* we have 3 parts: [aaa[abab]bbb]
380  * part a: a_ip1 <-> b_ip1 - 1
381  * part b: b_ip1 <-> a_ip2
382  * part c: a_ip2 + 1 <-> b_ip2
383  */
384  if (r == ADDRESS_LE) {
385  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
386  AddressCutIPv6CopySubOne(b_ip1, a->ip2.addr_data32);
387 
388  AddressCutIPv6Copy(b_ip1, b->ip.addr_data32);
389  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
390 
391  DetectAddress *tmp_c;
392  tmp_c = DetectAddressInit();
393  if (tmp_c == NULL)
394  goto error;
395  tmp_c->ip.family = AF_INET6;
396 
397  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip.addr_data32);
398  AddressCutIPv6Copy(b_ip2, tmp_c->ip2.addr_data32);
399 
400  *c = tmp_c;
401 
402  /* we have 3 parts: [bbb[baba]aaa]
403  * part a: b_ip1 <-> a_ip1 - 1
404  * part b: a_ip1 <-> b_ip2
405  * part c: b_ip2 + 1 <-> a_ip2
406  */
407  } else if (r == ADDRESS_GE) {
408  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
409  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
410 
411  AddressCutIPv6Copy(a_ip1, b->ip.addr_data32);
412  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
413 
414  DetectAddress *tmp_c;
415  tmp_c = DetectAddressInit();
416  if (tmp_c == NULL)
417  goto error;
418  tmp_c->ip.family = AF_INET6;
419 
420  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip.addr_data32);
421  AddressCutIPv6Copy(a_ip2, tmp_c->ip2.addr_data32);
422  *c = tmp_c;
423 
424  /* we have 2 or three parts:
425  *
426  * 2 part: [[abab]bbb] or [bbb[baba]]
427  * part a: a_ip1 <-> a_ip2
428  * part b: a_ip2 + 1 <-> b_ip2
429  *
430  * part a: b_ip1 <-> a_ip1 - 1
431  * part b: a_ip1 <-> a_ip2
432  *
433  * 3 part [bbb[aaa]bbb]
434  * part a: b_ip1 <-> a_ip1 - 1
435  * part b: a_ip1 <-> a_ip2
436  * part c: a_ip2 + 1 <-> b_ip2
437  */
438  } else if (r == ADDRESS_ES) {
439  if (AddressIPv6EqU32(a_ip1, b_ip1) == 1) {
440  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
441  AddressCutIPv6Copy(a_ip2, a->ip2.addr_data32);
442 
443  AddressCutIPv6CopyAddOne(a_ip2, b->ip.addr_data32);
444  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
445 
446  } else if (AddressIPv6EqU32(a_ip2, b_ip2) == 1) {
447  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
448  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
449 
450  AddressCutIPv6Copy(a_ip1, b->ip.addr_data32);
451  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
452 
453  } else {
454  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
455  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
456 
457  AddressCutIPv6Copy(a_ip1, b->ip.addr_data32);
458  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
459 
460  DetectAddress *tmp_c;
461  tmp_c = DetectAddressInit();
462  if (tmp_c == NULL) {
463  goto error;
464  }
465  tmp_c->ip.family = AF_INET6;
466  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip.addr_data32);
467  AddressCutIPv6Copy(b_ip2, tmp_c->ip2.addr_data32);
468  *c = tmp_c;
469 
470  }
471  /* we have 2 or three parts:
472  *
473  * 2 part: [[baba]aaa] or [aaa[abab]]
474  * part a: b_ip1 <-> b_ip2
475  * part b: b_ip2 + 1 <-> a_ip2
476  *
477  * part a: a_ip1 <-> b_ip1 - 1
478  * part b: b_ip1 <-> b_ip2
479  *
480  * 3 part [aaa[bbb]aaa]
481  * part a: a_ip1 <-> b_ip2 - 1
482  * part b: b_ip1 <-> b_ip2
483  * part c: b_ip2 + 1 <-> a_ip2
484  */
485  } else if (r == ADDRESS_EB) {
486  if (AddressIPv6EqU32(a_ip1, b_ip1) == 1) {
487  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
488  AddressCutIPv6Copy(b_ip2, a->ip2.addr_data32);
489 
490  AddressCutIPv6CopyAddOne(b_ip2, b->ip.addr_data32);
491  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
492  } else if (AddressIPv6EqU32(a_ip2, b_ip2) == 1) {
493  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
494  AddressCutIPv6CopySubOne(b_ip1, a->ip2.addr_data32);
495 
496  AddressCutIPv6Copy(b_ip1, b->ip.addr_data32);
497  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
498  } else {
499  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
500  AddressCutIPv6CopySubOne(b_ip1, a->ip2.addr_data32);
501 
502  AddressCutIPv6Copy(b_ip1, b->ip.addr_data32);
503  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
504 
505  DetectAddress *tmp_c;
506  tmp_c = DetectAddressInit();
507  if (tmp_c == NULL)
508  goto error;
509 
510  tmp_c->ip.family = AF_INET6;
511  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip.addr_data32);
512  AddressCutIPv6Copy(a_ip2, tmp_c->ip2.addr_data32);
513  *c = tmp_c;
514  }
515  }
516 
517  return 0;
518 
519 error:
520  return -1;
521 }
522 
523 #if 0
524 int DetectAddressCutIPv6(DetectAddressData *a, DetectAddressData *b,
525  DetectAddressData **c)
526 {
527  uint32_t a_ip1[4] = { SCNtohl(a->ip[0]), SCNtohl(a->ip[1]),
528  SCNtohl(a->ip[2]), SCNtohl(a->ip[3]) };
529  uint32_t a_ip2[4] = { SCNtohl(a->ip2[0]), SCNtohl(a->ip2[1]),
530  SCNtohl(a->ip2[2]), SCNtohl(a->ip2[3]) };
531  uint32_t b_ip1[4] = { SCNtohl(b->ip[0]), SCNtohl(b->ip[1]),
532  SCNtohl(b->ip[2]), SCNtohl(b->ip[3]) };
533  uint32_t b_ip2[4] = { SCNtohl(b->ip2[0]), SCNtohl(b->ip2[1]),
534  SCNtohl(b->ip2[2]), SCNtohl(b->ip2[3]) };
535 
536  /* default to NULL */
537  *c = NULL;
538 
539  int r = DetectAddressCmpIPv6(a, b);
540  if (r != ADDRESS_ES && r != ADDRESS_EB && r != ADDRESS_LE && r != ADDRESS_GE) {
541  goto error;
542  }
543 
544  /* we have 3 parts: [aaa[abab]bbb]
545  * part a: a_ip1 <-> b_ip1 - 1
546  * part b: b_ip1 <-> a_ip2
547  * part c: a_ip2 + 1 <-> b_ip2
548  */
549  if (r == ADDRESS_LE) {
550  AddressCutIPv6Copy(a_ip1, a->ip);
551  AddressCutIPv6CopySubOne(b_ip1, a->ip2);
552 
553  AddressCutIPv6Copy(b_ip1, b->ip);
554  AddressCutIPv6Copy(a_ip2, b->ip2);
555 
556  DetectAddressData *tmp_c;
557  tmp_c = DetectAddressDataInit();
558  if (tmp_c == NULL)
559  goto error;
560  tmp_c->family = AF_INET6;
561 
562  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip);
563  AddressCutIPv6Copy(b_ip2, tmp_c->ip2);
564 
565  *c = tmp_c;
566 
567  /* we have 3 parts: [bbb[baba]aaa]
568  * part a: b_ip1 <-> a_ip1 - 1
569  * part b: a_ip1 <-> b_ip2
570  * part c: b_ip2 + 1 <-> a_ip2
571  */
572  } else if (r == ADDRESS_GE) {
573  AddressCutIPv6Copy(b_ip1, a->ip);
574  AddressCutIPv6CopySubOne(a_ip1, a->ip2);
575 
576  AddressCutIPv6Copy(a_ip1, b->ip);
577  AddressCutIPv6Copy(b_ip2, b->ip2);
578 
579  DetectAddressData *tmp_c;
580  tmp_c = DetectAddressDataInit();
581  if (tmp_c == NULL)
582  goto error;
583  tmp_c->family = AF_INET6;
584 
585  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip);
586  AddressCutIPv6Copy(a_ip2, tmp_c->ip2);
587 
588  *c = tmp_c;
589 
590  /* we have 2 or three parts:
591  *
592  * 2 part: [[abab]bbb] or [bbb[baba]]
593  * part a: a_ip1 <-> a_ip2
594  * part b: a_ip2 + 1 <-> b_ip2
595  *
596  * part a: b_ip1 <-> a_ip1 - 1
597  * part b: a_ip1 <-> a_ip2
598  *
599  * 3 part [bbb[aaa]bbb]
600  * part a: b_ip1 <-> a_ip1 - 1
601  * part b: a_ip1 <-> a_ip2
602  * part c: a_ip2 + 1 <-> b_ip2
603  */
604  } else if (r == ADDRESS_ES) {
605  if (AddressIPv6Eq(a_ip1,b_ip1) == 1) {
606  AddressCutIPv6Copy(a_ip1, a->ip);
607  AddressCutIPv6Copy(a_ip2, a->ip2);
608 
609  AddressCutIPv6CopyAddOne(a_ip2, b->ip);
610  AddressCutIPv6Copy(b_ip2, b->ip2);
611  } else if (AddressIPv6Eq(a_ip2, b_ip2) == 1) {
612  AddressCutIPv6Copy(b_ip1, a->ip);
613  AddressCutIPv6CopySubOne(a_ip1, a->ip2);
614 
615  AddressCutIPv6Copy(a_ip1, b->ip);
616  AddressCutIPv6Copy(a_ip2, b->ip2);
617  } else {
618  AddressCutIPv6Copy(b_ip1, a->ip);
619  AddressCutIPv6CopySubOne(a_ip1, a->ip2);
620 
621  AddressCutIPv6Copy(a_ip1, b->ip);
622  AddressCutIPv6Copy(a_ip2, b->ip2);
623 
624  DetectAddressData *tmp_c;
625  tmp_c = DetectAddressDataInit();
626  if (tmp_c == NULL)
627  goto error;
628 
629  tmp_c->family = AF_INET6;
630 
631  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip);
632  AddressCutIPv6Copy(b_ip2, tmp_c->ip2);
633  *c = tmp_c;
634  }
635  /* we have 2 or three parts:
636  *
637  * 2 part: [[baba]aaa] or [aaa[abab]]
638  * part a: b_ip1 <-> b_ip2
639  * part b: b_ip2 + 1 <-> a_ip2
640  *
641  * part a: a_ip1 <-> b_ip1 - 1
642  * part b: b_ip1 <-> b_ip2
643  *
644  * 3 part [aaa[bbb]aaa]
645  * part a: a_ip1 <-> b_ip2 - 1
646  * part b: b_ip1 <-> b_ip2
647  * part c: b_ip2 + 1 <-> a_ip2
648  */
649  } else if (r == ADDRESS_EB) {
650  if (AddressIPv6Eq(a_ip1, b_ip1) == 1) {
651  AddressCutIPv6Copy(b_ip1, a->ip);
652  AddressCutIPv6Copy(b_ip2, a->ip2);
653 
654  AddressCutIPv6CopyAddOne(b_ip2, b->ip);
655  AddressCutIPv6Copy(a_ip2, b->ip2);
656  } else if (AddressIPv6Eq(a_ip2, b_ip2) == 1) {
657  AddressCutIPv6Copy(a_ip1, a->ip);
658  AddressCutIPv6CopySubOne(b_ip1, a->ip2);
659 
660  AddressCutIPv6Copy(b_ip1, b->ip);
661  AddressCutIPv6Copy(b_ip2, b->ip2);
662  } else {
663  AddressCutIPv6Copy(a_ip1, a->ip);
664  AddressCutIPv6CopySubOne(b_ip1, a->ip2);
665 
666  AddressCutIPv6Copy(b_ip1, b->ip);
667  AddressCutIPv6Copy(b_ip2, b->ip2);
668 
669  DetectAddressData *tmp_c;
670  tmp_c = DetectAddressDataInit();
671  if (tmp_c == NULL)
672  goto error;
673  tmp_c->family = AF_INET6;
674 
675  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip);
676  AddressCutIPv6Copy(a_ip2, tmp_c->ip2);
677  *c = tmp_c;
678  }
679  }
680 
681  return 0;
682 
683 error:
684  return -1;
685 }
686 #endif
687 
688 /**
689  * \brief Cuts and returns an address range, which is the complement of the
690  * address range that is supplied as the argument.
691  *
692  * For example:
693  *
694  * If a = ::-2000::,
695  * then a = 2000::1-FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF and b = NULL
696  * If a = 2000::1-FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF,
697  * then a = ::-2000:: and b = NULL
698  * If a = 2000::1-20FF::2,
699  * then a = ::-2000:: and
700  * b = 20FF::3-FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
701  *
702  * \param a Pointer to an address range (DetectAddress) instance whose complement
703  * has to be returned in a and b.
704  * \param b Pointer to DetectAddress pointer, that will be supplied back with a
705  * new DetectAddress instance, if the complement demands so.
706  *
707  * \retval 0 On success.
708  * \retval -1 On failure.
709  */
711 {
712  uint32_t a_ip1[4] = { SCNtohl(a->ip.addr_data32[0]), SCNtohl(a->ip.addr_data32[1]),
713  SCNtohl(a->ip.addr_data32[2]), SCNtohl(a->ip.addr_data32[3]) };
714  uint32_t a_ip2[4] = { SCNtohl(a->ip2.addr_data32[0]), SCNtohl(a->ip2.addr_data32[1]),
715  SCNtohl(a->ip2.addr_data32[2]), SCNtohl(a->ip2.addr_data32[3]) };
716  uint32_t ip_nul[4] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
717  uint32_t ip_max[4] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
718 
719  /* default to NULL */
720  *b = NULL;
721 
722  if (!(a_ip1[0] == 0x00000000 && a_ip1[1] == 0x00000000 &&
723  a_ip1[2] == 0x00000000 && a_ip1[3] == 0x00000000) &&
724  !(a_ip2[0] == 0xFFFFFFFF && a_ip2[1] == 0xFFFFFFFF &&
725  a_ip2[2] == 0xFFFFFFFF && a_ip2[3] == 0xFFFFFFFF)) {
726  AddressCutIPv6Copy(ip_nul, a->ip.addr_data32);
727  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
728 
729  DetectAddress *tmp_b = DetectAddressInit();
730  if (tmp_b == NULL)
731  goto error;
732 
733  tmp_b->ip.family = AF_INET6;
734  AddressCutIPv6CopyAddOne(a_ip2, tmp_b->ip.addr_data32);
735  AddressCutIPv6Copy(ip_max, tmp_b->ip2.addr_data32);
736  *b = tmp_b;
737  } else if ((a_ip1[0] == 0x00000000 && a_ip1[1] == 0x00000000 &&
738  a_ip1[2] == 0x00000000 && a_ip1[3] == 0x00000000) &&
739  !(a_ip2[0] == 0xFFFFFFFF && a_ip2[1] == 0xFFFFFFFF &&
740  a_ip2[2] == 0xFFFFFFFF && a_ip2[3] == 0xFFFFFFFF)) {
741  AddressCutIPv6CopyAddOne(a_ip2, a->ip.addr_data32);
742  AddressCutIPv6Copy(ip_max, a->ip2.addr_data32);
743  } else if (!(a_ip1[0] == 0x00000000 && a_ip1[1] == 0x00000000 &&
744  a_ip1[2] == 0x00000000 && a_ip1[3] == 0x00000000) &&
745  (a_ip2[0] == 0xFFFFFFFF && a_ip2[1] == 0xFFFFFFFF &&
746  a_ip2[2] == 0xFFFFFFFF && a_ip2[3] == 0xFFFFFFFF)) {
747  AddressCutIPv6Copy(ip_nul, a->ip.addr_data32);
748  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
749  } else {
750  goto error;
751  }
752 
753  return 0;
754 
755 error:
756  return -1;
757 }
758 
759 
760 /***************************************Unittests******************************/
761 
762 #ifdef UNITTESTS
763 
764 static int AddressTestIPv6Gt01(void)
765 {
766  int result = 0;
767 
768  uint32_t a[4] = { 1, 2, 3, 4 };
769  uint32_t b[4] = { 0, 2, 3, 4 };
770 
771  if (AddressIPv6GtU32(a, b) == 1)
772  result = 1;
773 
774  return result;
775 }
776 
777 static int AddressTestIPv6Gt02(void)
778 {
779  int result = 0;
780 
781  uint32_t a[4] = { 0, 2, 3, 4 };
782  uint32_t b[4] = { 1, 2, 3, 4 };
783 
784  if (AddressIPv6GtU32(a, b) == 0)
785  result = 1;
786 
787  return result;
788 }
789 
790 static int AddressTestIPv6Gt03(void)
791 {
792  int result = 0;
793 
794  uint32_t a[4] = { 1, 2, 3, 4 };
795  uint32_t b[4] = { 1, 2, 3, 4 };
796 
797  if (AddressIPv6GtU32(a, b) == 0)
798  result = 1;
799 
800  return result;
801 }
802 
803 static int AddressTestIPv6Gt04(void)
804 {
805  int result = 0;
806 
807  uint32_t a[4] = { 1, 2, 3, 5 };
808  uint32_t b[4] = { 1, 2, 3, 4 };
809 
810  if (AddressIPv6GtU32(a, b) == 1)
811  result = 1;
812 
813  return result;
814 }
815 
816 static int AddressTestIPv6Lt01(void)
817 {
818  int result = 0;
819 
820  uint32_t a[4] = { 0, 2, 3, 4 };
821  uint32_t b[4] = { 1, 2, 3, 4 };
822 
823  if (AddressIPv6LtU32(a, b) == 1)
824  result = 1;
825 
826  return result;
827 }
828 
829 static int AddressTestIPv6Lt02(void)
830 {
831  int result = 0;
832 
833  uint32_t a[4] = { 1, 2, 3, 4 };
834  uint32_t b[4] = { 0, 2, 3, 4 };
835 
836  if (AddressIPv6LtU32(a, b) == 0)
837  result = 1;
838 
839  return result;
840 }
841 
842 static int AddressTestIPv6Lt03(void)
843 {
844  int result = 0;
845 
846  uint32_t a[4] = { 1, 2, 3, 4 };
847  uint32_t b[4] = { 1, 2, 3, 4 };
848 
849  if (AddressIPv6LtU32(a, b) == 0)
850  result = 1;
851 
852  return result;
853 }
854 
855 static int AddressTestIPv6Lt04(void)
856 {
857  int result = 0;
858 
859  uint32_t a[4] = { 1, 2, 3, 4 };
860  uint32_t b[4] = { 1, 2, 3, 5 };
861 
862  if (AddressIPv6LtU32(a, b) == 1)
863  result = 1;
864 
865  return result;
866 }
867 
868 static int AddressTestIPv6Eq01(void)
869 {
870  int result = 0;
871 
872  uint32_t a[4] = { 0, 2, 3, 4 };
873  uint32_t b[4] = { 1, 2, 3, 4 };
874 
875  if (AddressIPv6EqU32(a, b) == 0)
876  result = 1;
877 
878  return result;
879 }
880 
881 static int AddressTestIPv6Eq02(void)
882 {
883  int result = 0;
884 
885  uint32_t a[4] = { 1, 2, 3, 4 };
886  uint32_t b[4] = { 0, 2, 3, 4 };
887 
888  if (AddressIPv6EqU32(a, b) == 0)
889  result = 1;
890 
891  return result;
892 }
893 
894 static int AddressTestIPv6Eq03(void)
895 {
896  int result = 0;
897 
898  uint32_t a[4] = { 1, 2, 3, 4 };
899  uint32_t b[4] = { 1, 2, 3, 4 };
900 
901  if (AddressIPv6EqU32(a, b) == 1)
902  result = 1;
903 
904  return result;
905 }
906 
907 static int AddressTestIPv6Eq04(void)
908 {
909  int result = 0;
910 
911  uint32_t a[4] = { 1, 2, 3, 4 };
912  uint32_t b[4] = { 1, 2, 3, 5 };
913 
914  if (AddressIPv6EqU32(a, b) == 0)
915  result = 1;
916 
917  return result;
918 }
919 
920 static int AddressTestIPv6Le01(void)
921 {
922  int result = 0;
923 
924  uint32_t a[4] = { 0, 2, 3, 4 };
925  uint32_t b[4] = { 1, 2, 3, 4 };
926 
927  if (AddressIPv6LeU32(a, b) == 1)
928  result = 1;
929 
930  return result;
931 }
932 
933 static int AddressTestIPv6Le02(void)
934 {
935  int result = 0;
936 
937  uint32_t a[4] = { 1, 2, 3, 4 };
938  uint32_t b[4] = { 0, 2, 3, 4 };
939 
940  if (AddressIPv6LeU32(a, b) == 0)
941  result = 1;
942 
943  return result;
944 }
945 
946 static int AddressTestIPv6Le03(void)
947 {
948  int result = 0;
949 
950  uint32_t a[4] = { 1, 2, 3, 4 };
951  uint32_t b[4] = { 1, 2, 3, 4 };
952 
953  if (AddressIPv6LeU32(a, b) == 1)
954  result = 1;
955 
956  return result;
957 }
958 
959 static int AddressTestIPv6Le04(void)
960 {
961  int result = 0;
962 
963  uint32_t a[4] = { 1, 2, 3, 4 };
964  uint32_t b[4] = { 1, 2, 3, 5 };
965 
966  if (AddressIPv6LeU32(a, b) == 1)
967  result = 1;
968 
969  return result;
970 }
971 
972 static int AddressTestIPv6Le05(void)
973 {
974  int result = 0;
975 
976  uint32_t a[4];
977  uint32_t b[4];
978  struct in6_addr in6;
979 
980  if (inet_pton(AF_INET6, "1999:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &in6) != 1)
981  return 0;
982  memcpy(&a, &in6.s6_addr, sizeof(in6.s6_addr));
983 
984  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
985  return 0;
986  memcpy(&b, &in6.s6_addr, sizeof(in6.s6_addr));
987 
988  if (AddressIPv6LeU32(a, b) == 1)
989  result = 1;
990 
991  return result;
992 }
993 
994 static int AddressTestIPv6Ge01(void)
995 {
996  int result = 0;
997 
998  uint32_t a[4] = { 0, 2, 3, 4 };
999  uint32_t b[4] = { 1, 2, 3, 4 };
1000 
1001  if (AddressIPv6GeU32(a, b) == 0)
1002  result = 1;
1003 
1004  return result;
1005 }
1006 
1007 static int AddressTestIPv6Ge02(void)
1008 {
1009  int result = 0;
1010 
1011  uint32_t a[4] = { 1, 2, 3, 4 };
1012  uint32_t b[4] = { 0, 2, 3, 4 };
1013 
1014  if (AddressIPv6GeU32(a, b) == 1)
1015  result = 1;
1016 
1017  return result;
1018 }
1019 
1020 static int AddressTestIPv6Ge03(void)
1021 {
1022  int result = 0;
1023 
1024  uint32_t a[4] = { 1, 2, 3, 4 };
1025  uint32_t b[4] = { 1, 2, 3, 4 };
1026 
1027  if (AddressIPv6GeU32(a, b) == 1)
1028  result = 1;
1029 
1030  return result;
1031 }
1032 
1033 static int AddressTestIPv6Ge04(void)
1034 {
1035  int result = 0;
1036 
1037  uint32_t a[4] = { 1, 2, 3, 4 };
1038  uint32_t b[4] = { 1, 2, 3, 5 };
1039 
1040  if (AddressIPv6GeU32(a, b) == 0)
1041  result = 1;
1042 
1043  return result;
1044 }
1045 
1046 static int AddressTestIPv6Ge05(void)
1047 {
1048  int result = 0;
1049 
1050  uint32_t a[4];
1051  uint32_t b[4];
1052  struct in6_addr in6;
1053 
1054  if (inet_pton(AF_INET6, "1999:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &in6) != 1)
1055  return 0;
1056  memcpy(&a, &in6.s6_addr, sizeof(in6.s6_addr));
1057 
1058  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1059  return 0;
1060  memcpy(&b, &in6.s6_addr, sizeof(in6.s6_addr));
1061 
1062  if (AddressIPv6GeU32(a, b) == 0)
1063  result = 1;
1064 
1065  return result;
1066 }
1067 
1068 static int AddressTestIPv6SubOne01(void)
1069 {
1070  int result = 0;
1071 
1072  uint32_t a[4], b[4];
1073  struct in6_addr in6;
1074 
1075  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1076  return 0;
1077  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1078 
1079  a[0] = SCNtohl(a[0]);
1080  a[1] = SCNtohl(a[1]);
1081  a[2] = SCNtohl(a[2]);
1082  a[3] = SCNtohl(a[3]);
1083 
1084  AddressCutIPv6CopySubOne(a, b);
1085 
1086  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1087  return 0;
1088  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1089  if (b[0] == a[0] && b[1] == a[1] &&
1090  b[2] == a[2] && b[3] == a[3]) {
1091  result = 1;
1092  }
1093 
1094  return result;
1095 }
1096 
1097 static int AddressTestIPv6SubOne02(void)
1098 {
1099  int result = 0;
1100 
1101  uint32_t a[4], b[4];
1102  struct in6_addr in6;
1103 
1104  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1105  return 0;
1106  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1107 
1108  a[0] = SCNtohl(a[0]);
1109  a[1] = SCNtohl(a[1]);
1110  a[2] = SCNtohl(a[2]);
1111  a[3] = SCNtohl(a[3]);
1112 
1113  AddressCutIPv6CopySubOne(a, b);
1114 
1115  if (inet_pton(AF_INET6, "1FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1116  return 0;
1117  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1118  if (b[0] == a[0] && b[1] == a[1] &&
1119  b[2] == a[2] && b[3] == a[3]) {
1120  result = 1;
1121  }
1122 
1123  return result;
1124 }
1125 
1126 static int AddressTestIPv6AddOne01(void)
1127 {
1128  int result = 0;
1129 
1130  uint32_t a[4], b[4];
1131  struct in6_addr in6;
1132 
1133  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1134  return 0;
1135  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1136 
1137  a[0] = SCNtohl(a[0]);
1138  a[1] = SCNtohl(a[1]);
1139  a[2] = SCNtohl(a[2]);
1140  a[3] = SCNtohl(a[3]);
1141 
1142  AddressCutIPv6CopyAddOne(a, b);
1143 
1144  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1145  return 0;
1146  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1147  if (b[0] == a[0] && b[1] == a[1] &&
1148  b[2] == a[2] && b[3] == a[3]) {
1149  result = 1;
1150  }
1151 
1152  return result;
1153 }
1154 
1155 static int AddressTestIPv6AddOne02(void)
1156 {
1157  int result = 0;
1158 
1159  uint32_t a[4], b[4];
1160  struct in6_addr in6;
1161 
1162  if (inet_pton(AF_INET6, "1FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1163  return 0;
1164  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1165 
1166  a[0] = SCNtohl(a[0]);
1167  a[1] = SCNtohl(a[1]);
1168  a[2] = SCNtohl(a[2]);
1169  a[3] = SCNtohl(a[3]);
1170 
1171  AddressCutIPv6CopyAddOne(a, b);
1172 
1173  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1174  return 0;
1175  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1176  if (b[0] == a[0] && b[1] == a[1] &&
1177  b[2] == a[2] && b[3] == a[3]) {
1178  result = 1;
1179  }
1180 
1181  return result;
1182 }
1183 
1184 static int AddressTestIPv6AddressCmp01(void)
1185 {
1188  struct in6_addr in6;
1189  int result = 1;
1190 
1191  if (a == NULL || b == NULL)
1192  goto error;
1193 
1194  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1195  goto error;
1196  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1197  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1198  goto error;
1199  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1200  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1201  goto error;
1202  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1203  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1204  goto error;
1205  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1206  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EQ);
1207 
1208  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1209  goto error;
1210  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1211  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1212  goto error;
1213  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1214  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1215  goto error;
1216  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1217  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1218  goto error;
1219  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1220  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1221 
1222  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1223  goto error;
1224  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1225  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1226  goto error;
1227  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1228  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1229  goto error;
1230  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1231  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1232  goto error;
1233  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1234  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1235 
1236  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1237  goto error;
1238  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1239  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1240  goto error;
1241  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1242  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1243  goto error;
1244  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1245  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1246  goto error;
1247  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1248  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1249 
1250  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1251  goto error;
1252  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1253  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1254  goto error;
1255  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1256  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1257  goto error;
1258  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1259  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1260  goto error;
1261  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1262  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1263 
1264  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1265  goto error;
1266  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1267  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1268  goto error;
1269  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1270  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1271  goto error;
1272  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1273  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1274  goto error;
1275  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1276  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_ES);
1277 
1278  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1279  goto error;
1280  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1281  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1282  goto error;
1283  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1284  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1285  goto error;
1286  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1287  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1288  goto error;
1289  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1290  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1291 
1292  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1293  goto error;
1294  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1295  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1296  goto error;
1297  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1298  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1299  goto error;
1300  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1301  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1302  goto error;
1303  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1304  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1305 
1306  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1307  goto error;
1308  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1309  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1310  goto error;
1311  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1312  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1313  goto error;
1314  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1315  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1316  goto error;
1317  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1318  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1319 
1320  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1321  goto error;
1322  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1323  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1324  goto error;
1325  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1326  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1327  goto error;
1328  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1329  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1330  goto error;
1331  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1332  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_EB);
1333 
1334  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1335  goto error;
1336  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1337  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1338  goto error;
1339  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1340  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1341  goto error;
1342  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1343  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1344  goto error;
1345  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1346  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1347 
1348  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1349  goto error;
1350  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1351  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1352  goto error;
1353  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1354  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1355  goto error;
1356  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1357  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1358  goto error;
1359  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1360  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1361 
1362  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1363  goto error;
1364  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1365  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1366  goto error;
1367  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1368  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1369  goto error;
1370  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1371  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1372  goto error;
1373  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1374  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LE);
1375 
1376  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1377  goto error;
1378  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1379  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1380  goto error;
1381  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1382  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1383  goto error;
1384  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1385  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1386  goto error;
1387  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1388  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1389 
1390  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1391  goto error;
1392  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1393  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1394  goto error;
1395  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1396  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1397  goto error;
1398  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1399  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1400  goto error;
1401  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1402  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LE);
1403 
1404  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1405  goto error;
1406  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1407  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1408  goto error;
1409  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1410  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1411  goto error;
1412  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1413  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1414  goto error;
1415  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1416  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LT);
1417 
1418  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1419  goto error;
1420  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1421  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1422  goto error;
1423  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1424  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1425  goto error;
1426  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1427  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1428  goto error;
1429  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1430  /* we could get a LE */
1431  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1432 
1433  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1434  goto error;
1435  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1436  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1437  goto error;
1438  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1439  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1440  goto error;
1441  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1442  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1443  goto error;
1444  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1445  /* we could get a LE */
1446  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1447 
1448  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1449  goto error;
1450  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1451  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1452  goto error;
1453  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1454  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1455  goto error;
1456  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1457  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1458  goto error;
1459  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1460  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1461 
1462  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1463  goto error;
1464  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1465  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1466  goto error;
1467  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1468  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1469  goto error;
1470  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1471  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1472  goto error;
1473  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1474  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1475 
1476  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1477  goto error;
1478  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1479  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1480  goto error;
1481  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1482  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1483  goto error;
1484  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1485  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1486  goto error;
1487  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1488  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1489 
1490  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1491  goto error;
1492  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1493  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1494  goto error;
1495  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1496  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1497  goto error;
1498  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1499  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1500  goto error;
1501  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1502  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1503 
1504  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1505  goto error;
1506  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1507  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1508  goto error;
1509  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1510  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1511  goto error;
1512  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1513  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1514  goto error;
1515  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1516  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1517 
1518  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1519  goto error;
1520  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1521  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1522  goto error;
1523  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1524  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1525  goto error;
1526  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1527  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1528  goto error;
1529  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1530  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1531 
1532  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1533  goto error;
1534  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1535  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1536  goto error;
1537  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1538  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1539  goto error;
1540  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1541  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1542  goto error;
1543  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1544  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1545 
1546  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1547  goto error;
1548  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1549  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1550  goto error;
1551  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1552  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1553  goto error;
1554  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1555  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1556  goto error;
1557  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1558  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1559 
1560  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1561  goto error;
1562  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1563  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1564  goto error;
1565  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1566  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1567  goto error;
1568  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1569  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1570  goto error;
1571  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1572  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1573 
1574  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1575  goto error;
1576  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1577  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1578  goto error;
1579  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1580  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1581  goto error;
1582  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1583  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1584  goto error;
1585  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1586  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GT);
1587 
1588  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1589  goto error;
1590  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1591  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1592  goto error;
1593  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1594  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1595  goto error;
1596  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1597  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1598  goto error;
1599  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1600  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GT);
1601 
1602  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1603  goto error;
1604  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1605  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1606  goto error;
1607  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1608  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1609  goto error;
1610  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1611  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1612  goto error;
1613  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1614  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GT);
1615 
1616  if (a != NULL)
1617  DetectAddressFree(a);
1618  if (b != NULL)
1619  DetectAddressFree(b);
1620  return result;
1621 
1622  error:
1623  if (a != NULL)
1624  DetectAddressFree(a);
1625  if (b != NULL)
1626  DetectAddressFree(b);
1627  return 0;
1628 }
1629 
1630 static int AddressTestIPv6CutNot01(void)
1631 {
1632  DetectAddress *a = NULL;
1633  DetectAddress *b = NULL;
1634  struct in6_addr in6;
1635  int result = 1;
1636 
1637  if ( (a = DetectAddressInit()) == NULL)
1638  goto error;
1639 
1640  if (inet_pton(AF_INET6, "::", &in6) != 1)
1641  goto error;
1642  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1643  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1644  goto error;
1645  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1646  result &= (DetectAddressCutNotIPv6(a, &b) == -1);
1647 
1648  if (a != NULL)
1649  DetectAddressFree(a);
1650  if (b != NULL)
1651  DetectAddressFree(b);
1652  return result;
1653 
1654  error:
1655  if (a != NULL)
1656  DetectAddressFree(a);
1657  if (b != NULL)
1658  DetectAddressFree(b);
1659  return 0;
1660 }
1661 
1662 static int AddressTestIPv6CutNot02(void)
1663 {
1664  DetectAddress *a = NULL;
1665  DetectAddress *b = NULL;
1666  DetectAddress *temp = NULL;
1667  struct in6_addr in6;
1668  int result = 1;
1669 
1670  if ( (a = DetectAddressInit()) == NULL)
1671  goto error;
1672  if ( (temp = DetectAddressInit()) == NULL)
1673  goto error;
1674 
1675  if (inet_pton(AF_INET6, "::", &in6) != 1)
1676  goto error;
1677  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1678  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1679  goto error;
1680  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1681  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1682 
1683  result &= (b == NULL);
1684 
1685  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1686  goto error;
1687  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1688  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1689  goto error;
1690  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1691 
1692  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1693 
1694  if (a != NULL)
1695  DetectAddressFree(a);
1696  if (b != NULL)
1697  DetectAddressFree(b);
1698  if (temp != NULL)
1699  DetectAddressFree(temp);
1700  return result;
1701 
1702  error:
1703  if (a != NULL)
1704  DetectAddressFree(a);
1705  if (b != NULL)
1706  DetectAddressFree(b);
1707  if (temp != NULL)
1708  DetectAddressFree(temp);
1709  return 0;
1710 }
1711 
1712 static int AddressTestIPv6CutNot03(void)
1713 {
1714  DetectAddress *a = NULL;
1715  DetectAddress *b = NULL;
1716  DetectAddress *temp = NULL;
1717  struct in6_addr in6;
1718  int result = 1;
1719 
1720  if ( (a = DetectAddressInit()) == NULL)
1721  goto error;
1722  if ( (temp = DetectAddressInit()) == NULL)
1723  goto error;
1724 
1725  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1726  goto error;
1727  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1728  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1729  goto error;
1730  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1731  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1732 
1733  result &= (b == NULL);
1734 
1735  if (inet_pton(AF_INET6, "::", &in6) != 1)
1736  goto error;
1737  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1738  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1739  goto error;
1740  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1741 
1742  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1743 
1744  if (a != NULL)
1745  DetectAddressFree(a);
1746  if (b != NULL)
1747  DetectAddressFree(b);
1748  if (temp != NULL)
1749  DetectAddressFree(temp);
1750  return result;
1751 
1752  error:
1753  if (a != NULL)
1754  DetectAddressFree(a);
1755  if (b != NULL)
1756  DetectAddressFree(b);
1757  if (temp != NULL)
1758  DetectAddressFree(temp);
1759  return 0;
1760 }
1761 
1762 static int AddressTestIPv6CutNot04(void)
1763 {
1764  DetectAddress *a = NULL;
1765  DetectAddress *b = NULL;
1766  DetectAddress *temp = NULL;
1767  struct in6_addr in6;
1768  int result = 1;
1769 
1770  if ( (a = DetectAddressInit()) == NULL)
1771  goto error;
1772  if ( (temp = DetectAddressInit()) == NULL)
1773  goto error;
1774 
1775  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1776  goto error;
1777  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1778  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1779  goto error;
1780  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1781  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1782 
1783  if (inet_pton(AF_INET6, "::", &in6) != 1)
1784  goto error;
1785  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1786  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1787  goto error;
1788  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1789  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1790 
1791  result &= (b != NULL);
1792  if (result == 0)
1793  goto error;
1794  if (inet_pton(AF_INET6, "2000::2", &in6) != 1)
1795  goto error;
1796  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1797  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1798  goto error;
1799  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1800  result = (DetectAddressCmpIPv6(b, temp) == ADDRESS_EQ);
1801 
1802  if (a != NULL)
1803  DetectAddressFree(a);
1804  if (b != NULL)
1805  DetectAddressFree(b);
1806  if (temp != NULL)
1807  DetectAddressFree(temp);
1808  return result;
1809 
1810  error:
1811  if (a != NULL)
1812  DetectAddressFree(a);
1813  if (b != NULL)
1814  DetectAddressFree(b);
1815  if (temp != NULL)
1816  DetectAddressFree(temp);
1817  return 0;
1818 }
1819 
1820 static int AddressTestIPv6CutNot05(void)
1821 {
1822  DetectAddress *a = NULL;
1823  DetectAddress *b = NULL;
1824  DetectAddress *temp = NULL;
1825  struct in6_addr in6;
1826  int result = 1;
1827 
1828  if ( (a = DetectAddressInit()) == NULL)
1829  goto error;
1830  if ( (temp = DetectAddressInit()) == NULL)
1831  goto error;
1832 
1833  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1834  goto error;
1835  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1836  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1837  goto error;
1838  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1839  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1840 
1841  if (inet_pton(AF_INET6, "::", &in6) != 1)
1842  goto error;
1843  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1844  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1845  goto error;
1846  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1847  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1848 
1849  result &= (b != NULL);
1850  if (result == 0)
1851  goto error;
1852  if (inet_pton(AF_INET6, "2000::21", &in6) != 1)
1853  goto error;
1854  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1855  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1856  goto error;
1857  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1858  result = (DetectAddressCmpIPv6(b, temp) == ADDRESS_EQ);
1859 
1860  if (a != NULL)
1861  DetectAddressFree(a);
1862  if (b != NULL)
1863  DetectAddressFree(b);
1864  if (temp != NULL)
1865  DetectAddressFree(temp);
1866  return result;
1867 
1868  error:
1869  if (a != NULL)
1870  DetectAddressFree(a);
1871  if (b != NULL)
1872  DetectAddressFree(b);
1873  if (temp != NULL)
1874  DetectAddressFree(temp);
1875  return 0;
1876 }
1877 
1878 #endif /* UNITTESTS */
1879 
1881 {
1882 
1883 #ifdef UNITTESTS
1884  UtRegisterTest("AddressTestIPv6Gt01", AddressTestIPv6Gt01);
1885  UtRegisterTest("AddressTestIPv6Gt02", AddressTestIPv6Gt02);
1886  UtRegisterTest("AddressTestIPv6Gt03", AddressTestIPv6Gt03);
1887  UtRegisterTest("AddressTestIPv6Gt04", AddressTestIPv6Gt04);
1888 
1889  UtRegisterTest("AddressTestIPv6Lt01", AddressTestIPv6Lt01);
1890  UtRegisterTest("AddressTestIPv6Lt02", AddressTestIPv6Lt02);
1891  UtRegisterTest("AddressTestIPv6Lt03", AddressTestIPv6Lt03);
1892  UtRegisterTest("AddressTestIPv6Lt04", AddressTestIPv6Lt04);
1893 
1894  UtRegisterTest("AddressTestIPv6Eq01", AddressTestIPv6Eq01);
1895  UtRegisterTest("AddressTestIPv6Eq02", AddressTestIPv6Eq02);
1896  UtRegisterTest("AddressTestIPv6Eq03", AddressTestIPv6Eq03);
1897  UtRegisterTest("AddressTestIPv6Eq04", AddressTestIPv6Eq04);
1898 
1899  UtRegisterTest("AddressTestIPv6Le01", AddressTestIPv6Le01);
1900  UtRegisterTest("AddressTestIPv6Le02", AddressTestIPv6Le02);
1901  UtRegisterTest("AddressTestIPv6Le03", AddressTestIPv6Le03);
1902  UtRegisterTest("AddressTestIPv6Le04", AddressTestIPv6Le04);
1903  UtRegisterTest("AddressTestIPv6Le05", AddressTestIPv6Le05);
1904 
1905  UtRegisterTest("AddressTestIPv6Ge01", AddressTestIPv6Ge01);
1906  UtRegisterTest("AddressTestIPv6Ge02", AddressTestIPv6Ge02);
1907  UtRegisterTest("AddressTestIPv6Ge03", AddressTestIPv6Ge03);
1908  UtRegisterTest("AddressTestIPv6Ge04", AddressTestIPv6Ge04);
1909  UtRegisterTest("AddressTestIPv6Ge05", AddressTestIPv6Ge05);
1910 
1911  UtRegisterTest("AddressTestIPv6SubOne01", AddressTestIPv6SubOne01);
1912  UtRegisterTest("AddressTestIPv6SubOne02", AddressTestIPv6SubOne02);
1913 
1914  UtRegisterTest("AddressTestIPv6AddOne01", AddressTestIPv6AddOne01);
1915  UtRegisterTest("AddressTestIPv6AddOne02", AddressTestIPv6AddOne02);
1916 
1917  UtRegisterTest("AddressTestIPv6AddressCmp01", AddressTestIPv6AddressCmp01);
1918 
1919  UtRegisterTest("AddressTestIPv6CutNot01", AddressTestIPv6CutNot01);
1920  UtRegisterTest("AddressTestIPv6CutNot02", AddressTestIPv6CutNot02);
1921  UtRegisterTest("AddressTestIPv6CutNot03", AddressTestIPv6CutNot03);
1922  UtRegisterTest("AddressTestIPv6CutNot04", AddressTestIPv6CutNot04);
1923  UtRegisterTest("AddressTestIPv6CutNot05", AddressTestIPv6CutNot05);
1924 #endif /* UNITTESTS */
1925 
1926  return;
1927 }
DetectAddress_::ip
Address ip
Definition: detect.h:164
DetectAddressFree
void DetectAddressFree(DetectAddress *ag)
Frees a DetectAddress instance.
Definition: detect-engine-address.c:82
DetectAddressCutIPv6
int DetectAddressCutIPv6(DetectEngineCtx *de_ctx, DetectAddress *a, DetectAddress *b, DetectAddress **c)
Definition: detect-engine-address-ipv6.c:359
ADDRESS_ER
@ ADDRESS_ER
Definition: detect.h:146
detect-engine-siggroup.h
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
DetectAddress_
address structure for use in the detection engine.
Definition: detect.h:162
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
AddressIPv6Gt
int AddressIPv6Gt(Address *a, Address *b)
Compares 2 ipv6 addresses and returns if the first address(a) is greater than the second address(b) o...
Definition: detect-engine-address-ipv6.c:90
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:836
Address_
Definition: decode.h:115
ADDRESS_LT
@ ADDRESS_LT
Definition: detect.h:147
util-unittest.h
util-cidr.h
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
ADDRESS_EB
@ ADDRESS_EB
Definition: detect.h:151
detect.h
detect-engine-port.h
DetectAddress_::ip2
Address ip2
Definition: detect.h:165
Address_::address
union Address_::@30 address
ADDRESS_EQ
@ ADDRESS_EQ
Definition: detect.h:149
ADDRESS_GE
@ ADDRESS_GE
Definition: detect.h:152
suricata-common.h
ADDRESS_GT
@ ADDRESS_GT
Definition: detect.h:153
AddressIPv6EqU32
int AddressIPv6EqU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:140
AddressIPv6GeU32
int AddressIPv6GeU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:205
ADDRESS_ES
@ ADDRESS_ES
Definition: detect.h:150
detect-engine-address-ipv6.h
AddressIPv6Ge
int AddressIPv6Ge(Address *a, Address *b)
Compares 2 ipv6 addresses and returns if the first address(a) is greater than or equal to the second ...
Definition: detect-engine-address-ipv6.c:194
DetectAddressCmpIPv6
int DetectAddressCmpIPv6(DetectAddress *a, DetectAddress *b)
Compares 2 addresses(address ranges) and returns the relationship between the 2 addresses.
Definition: detect-engine-address-ipv6.c:232
SCNtohl
#define SCNtohl(x)
Definition: suricata-common.h:413
AddressIPv6LeU32
int AddressIPv6LeU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:173
DetectAddressCutNotIPv6
int DetectAddressCutNotIPv6(DetectAddress *a, DetectAddress **b)
Cuts and returns an address range, which is the complement of the address range that is supplied as t...
Definition: detect-engine-address-ipv6.c:710
AddressIPv6Eq
int AddressIPv6Eq(Address *a, Address *b)
Compares 2 ipv6 addresses and returns if the addresses are equal or not.
Definition: detect-engine-address-ipv6.c:128
Address_::family
char family
Definition: decode.h:116
ADDRESS_LE
@ ADDRESS_LE
Definition: detect.h:148
AddressIPv6GtU32
int AddressIPv6GtU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:104
DetectAddressIPv6Tests
void DetectAddressIPv6Tests(void)
Definition: detect-engine-address-ipv6.c:1880
AddressIPv6LtU32
int AddressIPv6LtU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:66
flow-var.h
AddressIPv6Lt
int AddressIPv6Lt(Address *a, Address *b)
Compares 2 ipv6 addresses and returns if the first address(a) is less than the second address(b) or n...
Definition: detect-engine-address-ipv6.c:52
detect-engine-address.h
DetectAddressInit
DetectAddress * DetectAddressInit(void)
Creates and returns a new instance of a DetectAddress.
Definition: detect-engine-address.c:69
AddressIPv6Le
int AddressIPv6Le(Address *a, Address *b)
Compares 2 ipv6 addresses and returns if the first address(a) is less than or equal to the second add...
Definition: detect-engine-address-ipv6.c:162