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  DetectAddress *tmp = NULL;
372 
373  /* default to NULL */
374  *c = NULL;
375 
376  int r = DetectAddressCmpIPv6(a, b);
377  if (r != ADDRESS_ES && r != ADDRESS_EB && r != ADDRESS_LE && r != ADDRESS_GE) {
378  goto error;
379  }
380 
381  /* get a place to temporary put sigs lists */
382  tmp = DetectAddressInit();
383  if (tmp == NULL)
384  goto error;
385  memset(tmp,0,sizeof(DetectAddress));
386 
387  /* we have 3 parts: [aaa[abab]bbb]
388  * part a: a_ip1 <-> b_ip1 - 1
389  * part b: b_ip1 <-> a_ip2
390  * part c: a_ip2 + 1 <-> b_ip2
391  */
392  if (r == ADDRESS_LE) {
393  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
394  AddressCutIPv6CopySubOne(b_ip1, a->ip2.addr_data32);
395 
396  AddressCutIPv6Copy(b_ip1, b->ip.addr_data32);
397  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
398 
399  DetectAddress *tmp_c;
400  tmp_c = DetectAddressInit();
401  if (tmp_c == NULL)
402  goto error;
403  tmp_c->ip.family = AF_INET6;
404 
405  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip.addr_data32);
406  AddressCutIPv6Copy(b_ip2, tmp_c->ip2.addr_data32);
407 
408  *c = tmp_c;
409 
410  /* we have 3 parts: [bbb[baba]aaa]
411  * part a: b_ip1 <-> a_ip1 - 1
412  * part b: a_ip1 <-> b_ip2
413  * part c: b_ip2 + 1 <-> a_ip2
414  */
415  } else if (r == ADDRESS_GE) {
416  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
417  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
418 
419  AddressCutIPv6Copy(a_ip1, b->ip.addr_data32);
420  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
421 
422  DetectAddress *tmp_c;
423  tmp_c = DetectAddressInit();
424  if (tmp_c == NULL)
425  goto error;
426  tmp_c->ip.family = AF_INET6;
427 
428  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip.addr_data32);
429  AddressCutIPv6Copy(a_ip2, tmp_c->ip2.addr_data32);
430  *c = tmp_c;
431 
432  /* we have 2 or three parts:
433  *
434  * 2 part: [[abab]bbb] or [bbb[baba]]
435  * part a: a_ip1 <-> a_ip2
436  * part b: a_ip2 + 1 <-> b_ip2
437  *
438  * part a: b_ip1 <-> a_ip1 - 1
439  * part b: a_ip1 <-> a_ip2
440  *
441  * 3 part [bbb[aaa]bbb]
442  * part a: b_ip1 <-> a_ip1 - 1
443  * part b: a_ip1 <-> a_ip2
444  * part c: a_ip2 + 1 <-> b_ip2
445  */
446  } else if (r == ADDRESS_ES) {
447  if (AddressIPv6EqU32(a_ip1, b_ip1) == 1) {
448  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
449  AddressCutIPv6Copy(a_ip2, a->ip2.addr_data32);
450 
451  AddressCutIPv6CopyAddOne(a_ip2, b->ip.addr_data32);
452  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
453 
454  } else if (AddressIPv6EqU32(a_ip2, b_ip2) == 1) {
455  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
456  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
457 
458  AddressCutIPv6Copy(a_ip1, b->ip.addr_data32);
459  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
460 
461  } else {
462  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
463  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
464 
465  AddressCutIPv6Copy(a_ip1, b->ip.addr_data32);
466  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
467 
468  DetectAddress *tmp_c;
469  tmp_c = DetectAddressInit();
470  if (tmp_c == NULL) {
471  goto error;
472  }
473  tmp_c->ip.family = AF_INET6;
474  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip.addr_data32);
475  AddressCutIPv6Copy(b_ip2, tmp_c->ip2.addr_data32);
476  *c = tmp_c;
477 
478  }
479  /* we have 2 or three parts:
480  *
481  * 2 part: [[baba]aaa] or [aaa[abab]]
482  * part a: b_ip1 <-> b_ip2
483  * part b: b_ip2 + 1 <-> a_ip2
484  *
485  * part a: a_ip1 <-> b_ip1 - 1
486  * part b: b_ip1 <-> b_ip2
487  *
488  * 3 part [aaa[bbb]aaa]
489  * part a: a_ip1 <-> b_ip2 - 1
490  * part b: b_ip1 <-> b_ip2
491  * part c: b_ip2 + 1 <-> a_ip2
492  */
493  } else if (r == ADDRESS_EB) {
494  if (AddressIPv6EqU32(a_ip1, b_ip1) == 1) {
495  AddressCutIPv6Copy(b_ip1, a->ip.addr_data32);
496  AddressCutIPv6Copy(b_ip2, a->ip2.addr_data32);
497 
498  AddressCutIPv6CopyAddOne(b_ip2, b->ip.addr_data32);
499  AddressCutIPv6Copy(a_ip2, b->ip2.addr_data32);
500  } else if (AddressIPv6EqU32(a_ip2, b_ip2) == 1) {
501  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
502  AddressCutIPv6CopySubOne(b_ip1, a->ip2.addr_data32);
503 
504  AddressCutIPv6Copy(b_ip1, b->ip.addr_data32);
505  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
506  } else {
507  AddressCutIPv6Copy(a_ip1, a->ip.addr_data32);
508  AddressCutIPv6CopySubOne(b_ip1, a->ip2.addr_data32);
509 
510  AddressCutIPv6Copy(b_ip1, b->ip.addr_data32);
511  AddressCutIPv6Copy(b_ip2, b->ip2.addr_data32);
512 
513  DetectAddress *tmp_c;
514  tmp_c = DetectAddressInit();
515  if (tmp_c == NULL)
516  goto error;
517 
518  tmp_c->ip.family = AF_INET6;
519  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip.addr_data32);
520  AddressCutIPv6Copy(a_ip2, tmp_c->ip2.addr_data32);
521  *c = tmp_c;
522  }
523  }
524 
525  if (tmp != NULL)
526  DetectAddressFree(tmp);
527 
528  return 0;
529 
530 error:
531  if (tmp != NULL)
532  DetectAddressFree(tmp);
533  return -1;
534 }
535 
536 #if 0
537 int DetectAddressCutIPv6(DetectAddressData *a, DetectAddressData *b,
538  DetectAddressData **c)
539 {
540  uint32_t a_ip1[4] = { SCNtohl(a->ip[0]), SCNtohl(a->ip[1]),
541  SCNtohl(a->ip[2]), SCNtohl(a->ip[3]) };
542  uint32_t a_ip2[4] = { SCNtohl(a->ip2[0]), SCNtohl(a->ip2[1]),
543  SCNtohl(a->ip2[2]), SCNtohl(a->ip2[3]) };
544  uint32_t b_ip1[4] = { SCNtohl(b->ip[0]), SCNtohl(b->ip[1]),
545  SCNtohl(b->ip[2]), SCNtohl(b->ip[3]) };
546  uint32_t b_ip2[4] = { SCNtohl(b->ip2[0]), SCNtohl(b->ip2[1]),
547  SCNtohl(b->ip2[2]), SCNtohl(b->ip2[3]) };
548 
549  /* default to NULL */
550  *c = NULL;
551 
552  int r = DetectAddressCmpIPv6(a, b);
553  if (r != ADDRESS_ES && r != ADDRESS_EB && r != ADDRESS_LE && r != ADDRESS_GE) {
554  goto error;
555  }
556 
557  /* we have 3 parts: [aaa[abab]bbb]
558  * part a: a_ip1 <-> b_ip1 - 1
559  * part b: b_ip1 <-> a_ip2
560  * part c: a_ip2 + 1 <-> b_ip2
561  */
562  if (r == ADDRESS_LE) {
563  AddressCutIPv6Copy(a_ip1, a->ip);
564  AddressCutIPv6CopySubOne(b_ip1, a->ip2);
565 
566  AddressCutIPv6Copy(b_ip1, b->ip);
567  AddressCutIPv6Copy(a_ip2, b->ip2);
568 
569  DetectAddressData *tmp_c;
570  tmp_c = DetectAddressDataInit();
571  if (tmp_c == NULL)
572  goto error;
573  tmp_c->family = AF_INET6;
574 
575  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip);
576  AddressCutIPv6Copy(b_ip2, tmp_c->ip2);
577 
578  *c = tmp_c;
579 
580  /* we have 3 parts: [bbb[baba]aaa]
581  * part a: b_ip1 <-> a_ip1 - 1
582  * part b: a_ip1 <-> b_ip2
583  * part c: b_ip2 + 1 <-> a_ip2
584  */
585  } else if (r == ADDRESS_GE) {
586  AddressCutIPv6Copy(b_ip1, a->ip);
587  AddressCutIPv6CopySubOne(a_ip1, a->ip2);
588 
589  AddressCutIPv6Copy(a_ip1, b->ip);
590  AddressCutIPv6Copy(b_ip2, b->ip2);
591 
592  DetectAddressData *tmp_c;
593  tmp_c = DetectAddressDataInit();
594  if (tmp_c == NULL)
595  goto error;
596  tmp_c->family = AF_INET6;
597 
598  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip);
599  AddressCutIPv6Copy(a_ip2, tmp_c->ip2);
600 
601  *c = tmp_c;
602 
603  /* we have 2 or three parts:
604  *
605  * 2 part: [[abab]bbb] or [bbb[baba]]
606  * part a: a_ip1 <-> a_ip2
607  * part b: a_ip2 + 1 <-> b_ip2
608  *
609  * part a: b_ip1 <-> a_ip1 - 1
610  * part b: a_ip1 <-> a_ip2
611  *
612  * 3 part [bbb[aaa]bbb]
613  * part a: b_ip1 <-> a_ip1 - 1
614  * part b: a_ip1 <-> a_ip2
615  * part c: a_ip2 + 1 <-> b_ip2
616  */
617  } else if (r == ADDRESS_ES) {
618  if (AddressIPv6Eq(a_ip1,b_ip1) == 1) {
619  AddressCutIPv6Copy(a_ip1, a->ip);
620  AddressCutIPv6Copy(a_ip2, a->ip2);
621 
622  AddressCutIPv6CopyAddOne(a_ip2, b->ip);
623  AddressCutIPv6Copy(b_ip2, b->ip2);
624  } else if (AddressIPv6Eq(a_ip2, b_ip2) == 1) {
625  AddressCutIPv6Copy(b_ip1, a->ip);
626  AddressCutIPv6CopySubOne(a_ip1, a->ip2);
627 
628  AddressCutIPv6Copy(a_ip1, b->ip);
629  AddressCutIPv6Copy(a_ip2, b->ip2);
630  } else {
631  AddressCutIPv6Copy(b_ip1, a->ip);
632  AddressCutIPv6CopySubOne(a_ip1, a->ip2);
633 
634  AddressCutIPv6Copy(a_ip1, b->ip);
635  AddressCutIPv6Copy(a_ip2, b->ip2);
636 
637  DetectAddressData *tmp_c;
638  tmp_c = DetectAddressDataInit();
639  if (tmp_c == NULL)
640  goto error;
641 
642  tmp_c->family = AF_INET6;
643 
644  AddressCutIPv6CopyAddOne(a_ip2, tmp_c->ip);
645  AddressCutIPv6Copy(b_ip2, tmp_c->ip2);
646  *c = tmp_c;
647  }
648  /* we have 2 or three parts:
649  *
650  * 2 part: [[baba]aaa] or [aaa[abab]]
651  * part a: b_ip1 <-> b_ip2
652  * part b: b_ip2 + 1 <-> a_ip2
653  *
654  * part a: a_ip1 <-> b_ip1 - 1
655  * part b: b_ip1 <-> b_ip2
656  *
657  * 3 part [aaa[bbb]aaa]
658  * part a: a_ip1 <-> b_ip2 - 1
659  * part b: b_ip1 <-> b_ip2
660  * part c: b_ip2 + 1 <-> a_ip2
661  */
662  } else if (r == ADDRESS_EB) {
663  if (AddressIPv6Eq(a_ip1, b_ip1) == 1) {
664  AddressCutIPv6Copy(b_ip1, a->ip);
665  AddressCutIPv6Copy(b_ip2, a->ip2);
666 
667  AddressCutIPv6CopyAddOne(b_ip2, b->ip);
668  AddressCutIPv6Copy(a_ip2, b->ip2);
669  } else if (AddressIPv6Eq(a_ip2, b_ip2) == 1) {
670  AddressCutIPv6Copy(a_ip1, a->ip);
671  AddressCutIPv6CopySubOne(b_ip1, a->ip2);
672 
673  AddressCutIPv6Copy(b_ip1, b->ip);
674  AddressCutIPv6Copy(b_ip2, b->ip2);
675  } else {
676  AddressCutIPv6Copy(a_ip1, a->ip);
677  AddressCutIPv6CopySubOne(b_ip1, a->ip2);
678 
679  AddressCutIPv6Copy(b_ip1, b->ip);
680  AddressCutIPv6Copy(b_ip2, b->ip2);
681 
682  DetectAddressData *tmp_c;
683  tmp_c = DetectAddressDataInit();
684  if (tmp_c == NULL)
685  goto error;
686  tmp_c->family = AF_INET6;
687 
688  AddressCutIPv6CopyAddOne(b_ip2, tmp_c->ip);
689  AddressCutIPv6Copy(a_ip2, tmp_c->ip2);
690  *c = tmp_c;
691  }
692  }
693 
694  return 0;
695 
696 error:
697  return -1;
698 }
699 #endif
700 
701 /**
702  * \brief Cuts and returns an address range, which is the complement of the
703  * address range that is supplied as the argument.
704  *
705  * For example:
706  *
707  * If a = ::-2000::,
708  * then a = 2000::1-FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF and b = NULL
709  * If a = 2000::1-FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF,
710  * then a = ::-2000:: and b = NULL
711  * If a = 2000::1-20FF::2,
712  * then a = ::-2000:: and
713  * b = 20FF::3-FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
714  *
715  * \param a Pointer to an address range (DetectAddress) instance whose complement
716  * has to be returned in a and b.
717  * \param b Pointer to DetectAddress pointer, that will be supplied back with a
718  * new DetectAddress instance, if the complement demands so.
719  *
720  * \retval 0 On success.
721  * \retval -1 On failure.
722  */
724 {
725  uint32_t a_ip1[4] = { SCNtohl(a->ip.addr_data32[0]), SCNtohl(a->ip.addr_data32[1]),
726  SCNtohl(a->ip.addr_data32[2]), SCNtohl(a->ip.addr_data32[3]) };
727  uint32_t a_ip2[4] = { SCNtohl(a->ip2.addr_data32[0]), SCNtohl(a->ip2.addr_data32[1]),
728  SCNtohl(a->ip2.addr_data32[2]), SCNtohl(a->ip2.addr_data32[3]) };
729  uint32_t ip_nul[4] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
730  uint32_t ip_max[4] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
731 
732  /* default to NULL */
733  *b = NULL;
734 
735  if (!(a_ip1[0] == 0x00000000 && a_ip1[1] == 0x00000000 &&
736  a_ip1[2] == 0x00000000 && a_ip1[3] == 0x00000000) &&
737  !(a_ip2[0] == 0xFFFFFFFF && a_ip2[1] == 0xFFFFFFFF &&
738  a_ip2[2] == 0xFFFFFFFF && a_ip2[3] == 0xFFFFFFFF)) {
739  AddressCutIPv6Copy(ip_nul, a->ip.addr_data32);
740  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
741 
742  DetectAddress *tmp_b = DetectAddressInit();
743  if (tmp_b == NULL)
744  goto error;
745 
746  tmp_b->ip.family = AF_INET6;
747  AddressCutIPv6CopyAddOne(a_ip2, tmp_b->ip.addr_data32);
748  AddressCutIPv6Copy(ip_max, tmp_b->ip2.addr_data32);
749  *b = tmp_b;
750  } else if ((a_ip1[0] == 0x00000000 && a_ip1[1] == 0x00000000 &&
751  a_ip1[2] == 0x00000000 && a_ip1[3] == 0x00000000) &&
752  !(a_ip2[0] == 0xFFFFFFFF && a_ip2[1] == 0xFFFFFFFF &&
753  a_ip2[2] == 0xFFFFFFFF && a_ip2[3] == 0xFFFFFFFF)) {
754  AddressCutIPv6CopyAddOne(a_ip2, a->ip.addr_data32);
755  AddressCutIPv6Copy(ip_max, a->ip2.addr_data32);
756  } else if (!(a_ip1[0] == 0x00000000 && a_ip1[1] == 0x00000000 &&
757  a_ip1[2] == 0x00000000 && a_ip1[3] == 0x00000000) &&
758  (a_ip2[0] == 0xFFFFFFFF && a_ip2[1] == 0xFFFFFFFF &&
759  a_ip2[2] == 0xFFFFFFFF && a_ip2[3] == 0xFFFFFFFF)) {
760  AddressCutIPv6Copy(ip_nul, a->ip.addr_data32);
761  AddressCutIPv6CopySubOne(a_ip1, a->ip2.addr_data32);
762  } else {
763  goto error;
764  }
765 
766  return 0;
767 
768 error:
769  return -1;
770 }
771 
772 
773 /***************************************Unittests******************************/
774 
775 #ifdef UNITTESTS
776 
777 static int AddressTestIPv6Gt01(void)
778 {
779  int result = 0;
780 
781  uint32_t a[4] = { 1, 2, 3, 4 };
782  uint32_t b[4] = { 0, 2, 3, 4 };
783 
784  if (AddressIPv6GtU32(a, b) == 1)
785  result = 1;
786 
787  return result;
788 }
789 
790 static int AddressTestIPv6Gt02(void)
791 {
792  int result = 0;
793 
794  uint32_t a[4] = { 0, 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 AddressTestIPv6Gt03(void)
804 {
805  int result = 0;
806 
807  uint32_t a[4] = { 1, 2, 3, 4 };
808  uint32_t b[4] = { 1, 2, 3, 4 };
809 
810  if (AddressIPv6GtU32(a, b) == 0)
811  result = 1;
812 
813  return result;
814 }
815 
816 static int AddressTestIPv6Gt04(void)
817 {
818  int result = 0;
819 
820  uint32_t a[4] = { 1, 2, 3, 5 };
821  uint32_t b[4] = { 1, 2, 3, 4 };
822 
823  if (AddressIPv6GtU32(a, b) == 1)
824  result = 1;
825 
826  return result;
827 }
828 
829 static int AddressTestIPv6Lt01(void)
830 {
831  int result = 0;
832 
833  uint32_t a[4] = { 0, 2, 3, 4 };
834  uint32_t b[4] = { 1, 2, 3, 4 };
835 
836  if (AddressIPv6LtU32(a, b) == 1)
837  result = 1;
838 
839  return result;
840 }
841 
842 static int AddressTestIPv6Lt02(void)
843 {
844  int result = 0;
845 
846  uint32_t a[4] = { 1, 2, 3, 4 };
847  uint32_t b[4] = { 0, 2, 3, 4 };
848 
849  if (AddressIPv6LtU32(a, b) == 0)
850  result = 1;
851 
852  return result;
853 }
854 
855 static int AddressTestIPv6Lt03(void)
856 {
857  int result = 0;
858 
859  uint32_t a[4] = { 1, 2, 3, 4 };
860  uint32_t b[4] = { 1, 2, 3, 4 };
861 
862  if (AddressIPv6LtU32(a, b) == 0)
863  result = 1;
864 
865  return result;
866 }
867 
868 static int AddressTestIPv6Lt04(void)
869 {
870  int result = 0;
871 
872  uint32_t a[4] = { 1, 2, 3, 4 };
873  uint32_t b[4] = { 1, 2, 3, 5 };
874 
875  if (AddressIPv6LtU32(a, b) == 1)
876  result = 1;
877 
878  return result;
879 }
880 
881 static int AddressTestIPv6Eq01(void)
882 {
883  int result = 0;
884 
885  uint32_t a[4] = { 0, 2, 3, 4 };
886  uint32_t b[4] = { 1, 2, 3, 4 };
887 
888  if (AddressIPv6EqU32(a, b) == 0)
889  result = 1;
890 
891  return result;
892 }
893 
894 static int AddressTestIPv6Eq02(void)
895 {
896  int result = 0;
897 
898  uint32_t a[4] = { 1, 2, 3, 4 };
899  uint32_t b[4] = { 0, 2, 3, 4 };
900 
901  if (AddressIPv6EqU32(a, b) == 0)
902  result = 1;
903 
904  return result;
905 }
906 
907 static int AddressTestIPv6Eq03(void)
908 {
909  int result = 0;
910 
911  uint32_t a[4] = { 1, 2, 3, 4 };
912  uint32_t b[4] = { 1, 2, 3, 4 };
913 
914  if (AddressIPv6EqU32(a, b) == 1)
915  result = 1;
916 
917  return result;
918 }
919 
920 static int AddressTestIPv6Eq04(void)
921 {
922  int result = 0;
923 
924  uint32_t a[4] = { 1, 2, 3, 4 };
925  uint32_t b[4] = { 1, 2, 3, 5 };
926 
927  if (AddressIPv6EqU32(a, b) == 0)
928  result = 1;
929 
930  return result;
931 }
932 
933 static int AddressTestIPv6Le01(void)
934 {
935  int result = 0;
936 
937  uint32_t a[4] = { 0, 2, 3, 4 };
938  uint32_t b[4] = { 1, 2, 3, 4 };
939 
940  if (AddressIPv6LeU32(a, b) == 1)
941  result = 1;
942 
943  return result;
944 }
945 
946 static int AddressTestIPv6Le02(void)
947 {
948  int result = 0;
949 
950  uint32_t a[4] = { 1, 2, 3, 4 };
951  uint32_t b[4] = { 0, 2, 3, 4 };
952 
953  if (AddressIPv6LeU32(a, b) == 0)
954  result = 1;
955 
956  return result;
957 }
958 
959 static int AddressTestIPv6Le03(void)
960 {
961  int result = 0;
962 
963  uint32_t a[4] = { 1, 2, 3, 4 };
964  uint32_t b[4] = { 1, 2, 3, 4 };
965 
966  if (AddressIPv6LeU32(a, b) == 1)
967  result = 1;
968 
969  return result;
970 }
971 
972 static int AddressTestIPv6Le04(void)
973 {
974  int result = 0;
975 
976  uint32_t a[4] = { 1, 2, 3, 4 };
977  uint32_t b[4] = { 1, 2, 3, 5 };
978 
979  if (AddressIPv6LeU32(a, b) == 1)
980  result = 1;
981 
982  return result;
983 }
984 
985 static int AddressTestIPv6Le05(void)
986 {
987  int result = 0;
988 
989  uint32_t a[4];
990  uint32_t b[4];
991  struct in6_addr in6;
992 
993  if (inet_pton(AF_INET6, "1999:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &in6) != 1)
994  return 0;
995  memcpy(&a, &in6.s6_addr, sizeof(in6.s6_addr));
996 
997  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
998  return 0;
999  memcpy(&b, &in6.s6_addr, sizeof(in6.s6_addr));
1000 
1001  if (AddressIPv6LeU32(a, b) == 1)
1002  result = 1;
1003 
1004  return result;
1005 }
1006 
1007 static int AddressTestIPv6Ge01(void)
1008 {
1009  int result = 0;
1010 
1011  uint32_t a[4] = { 0, 2, 3, 4 };
1012  uint32_t b[4] = { 1, 2, 3, 4 };
1013 
1014  if (AddressIPv6GeU32(a, b) == 0)
1015  result = 1;
1016 
1017  return result;
1018 }
1019 
1020 static int AddressTestIPv6Ge02(void)
1021 {
1022  int result = 0;
1023 
1024  uint32_t a[4] = { 1, 2, 3, 4 };
1025  uint32_t b[4] = { 0, 2, 3, 4 };
1026 
1027  if (AddressIPv6GeU32(a, b) == 1)
1028  result = 1;
1029 
1030  return result;
1031 }
1032 
1033 static int AddressTestIPv6Ge03(void)
1034 {
1035  int result = 0;
1036 
1037  uint32_t a[4] = { 1, 2, 3, 4 };
1038  uint32_t b[4] = { 1, 2, 3, 4 };
1039 
1040  if (AddressIPv6GeU32(a, b) == 1)
1041  result = 1;
1042 
1043  return result;
1044 }
1045 
1046 static int AddressTestIPv6Ge04(void)
1047 {
1048  int result = 0;
1049 
1050  uint32_t a[4] = { 1, 2, 3, 4 };
1051  uint32_t b[4] = { 1, 2, 3, 5 };
1052 
1053  if (AddressIPv6GeU32(a, b) == 0)
1054  result = 1;
1055 
1056  return result;
1057 }
1058 
1059 static int AddressTestIPv6Ge05(void)
1060 {
1061  int result = 0;
1062 
1063  uint32_t a[4];
1064  uint32_t b[4];
1065  struct in6_addr in6;
1066 
1067  if (inet_pton(AF_INET6, "1999:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &in6) != 1)
1068  return 0;
1069  memcpy(&a, &in6.s6_addr, sizeof(in6.s6_addr));
1070 
1071  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1072  return 0;
1073  memcpy(&b, &in6.s6_addr, sizeof(in6.s6_addr));
1074 
1075  if (AddressIPv6GeU32(a, b) == 0)
1076  result = 1;
1077 
1078  return result;
1079 }
1080 
1081 static int AddressTestIPv6SubOne01(void)
1082 {
1083  int result = 0;
1084 
1085  uint32_t a[4], b[4];
1086  struct in6_addr in6;
1087 
1088  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1089  return 0;
1090  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1091 
1092  a[0] = SCNtohl(a[0]);
1093  a[1] = SCNtohl(a[1]);
1094  a[2] = SCNtohl(a[2]);
1095  a[3] = SCNtohl(a[3]);
1096 
1097  AddressCutIPv6CopySubOne(a, b);
1098 
1099  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1100  return 0;
1101  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1102  if (b[0] == a[0] && b[1] == a[1] &&
1103  b[2] == a[2] && b[3] == a[3]) {
1104  result = 1;
1105  }
1106 
1107  return result;
1108 }
1109 
1110 static int AddressTestIPv6SubOne02(void)
1111 {
1112  int result = 0;
1113 
1114  uint32_t a[4], b[4];
1115  struct in6_addr in6;
1116 
1117  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1118  return 0;
1119  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1120 
1121  a[0] = SCNtohl(a[0]);
1122  a[1] = SCNtohl(a[1]);
1123  a[2] = SCNtohl(a[2]);
1124  a[3] = SCNtohl(a[3]);
1125 
1126  AddressCutIPv6CopySubOne(a, b);
1127 
1128  if (inet_pton(AF_INET6, "1FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1129  return 0;
1130  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1131  if (b[0] == a[0] && b[1] == a[1] &&
1132  b[2] == a[2] && b[3] == a[3]) {
1133  result = 1;
1134  }
1135 
1136  return result;
1137 }
1138 
1139 static int AddressTestIPv6AddOne01(void)
1140 {
1141  int result = 0;
1142 
1143  uint32_t a[4], b[4];
1144  struct in6_addr in6;
1145 
1146  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1147  return 0;
1148  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1149 
1150  a[0] = SCNtohl(a[0]);
1151  a[1] = SCNtohl(a[1]);
1152  a[2] = SCNtohl(a[2]);
1153  a[3] = SCNtohl(a[3]);
1154 
1155  AddressCutIPv6CopyAddOne(a, b);
1156 
1157  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1158  return 0;
1159  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1160  if (b[0] == a[0] && b[1] == a[1] &&
1161  b[2] == a[2] && b[3] == a[3]) {
1162  result = 1;
1163  }
1164 
1165  return result;
1166 }
1167 
1168 static int AddressTestIPv6AddOne02(void)
1169 {
1170  int result = 0;
1171 
1172  uint32_t a[4], b[4];
1173  struct in6_addr in6;
1174 
1175  if (inet_pton(AF_INET6, "1FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1176  return 0;
1177  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1178 
1179  a[0] = SCNtohl(a[0]);
1180  a[1] = SCNtohl(a[1]);
1181  a[2] = SCNtohl(a[2]);
1182  a[3] = SCNtohl(a[3]);
1183 
1184  AddressCutIPv6CopyAddOne(a, b);
1185 
1186  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1187  return 0;
1188  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1189  if (b[0] == a[0] && b[1] == a[1] &&
1190  b[2] == a[2] && b[3] == a[3]) {
1191  result = 1;
1192  }
1193 
1194  return result;
1195 }
1196 
1197 static int AddressTestIPv6AddressCmp01(void)
1198 {
1201  struct in6_addr in6;
1202  int result = 1;
1203 
1204  if (a == NULL || b == NULL)
1205  goto error;
1206 
1207  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1208  goto error;
1209  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1210  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1211  goto error;
1212  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1213  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1214  goto error;
1215  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1216  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1217  goto error;
1218  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1219  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EQ);
1220 
1221  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1222  goto error;
1223  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1224  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1225  goto error;
1226  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1227  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1228  goto error;
1229  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1230  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1231  goto error;
1232  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1233  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1234 
1235  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1236  goto error;
1237  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1238  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1239  goto error;
1240  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1241  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1242  goto error;
1243  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1244  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1245  goto error;
1246  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1247  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1248 
1249  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1250  goto error;
1251  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1252  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1253  goto error;
1254  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1255  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1256  goto error;
1257  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1258  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1259  goto error;
1260  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1261  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1262 
1263  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1264  goto error;
1265  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1266  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1267  goto error;
1268  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1269  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1270  goto error;
1271  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1272  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1273  goto error;
1274  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1275  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1276 
1277  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1278  goto error;
1279  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1280  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1281  goto error;
1282  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1283  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1284  goto error;
1285  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1286  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1287  goto error;
1288  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1289  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_ES);
1290 
1291  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1292  goto error;
1293  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1294  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1295  goto error;
1296  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1297  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1298  goto error;
1299  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1300  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1301  goto error;
1302  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1303  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1304 
1305  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1306  goto error;
1307  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1308  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1309  goto error;
1310  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1311  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1312  goto error;
1313  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1314  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1315  goto error;
1316  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1317  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1318 
1319  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1320  goto error;
1321  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1322  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1323  goto error;
1324  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1325  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1326  goto error;
1327  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1328  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1329  goto error;
1330  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1331  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1332 
1333  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1334  goto error;
1335  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1336  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1337  goto error;
1338  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1339  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1340  goto error;
1341  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1342  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1343  goto error;
1344  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1345  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_EB);
1346 
1347  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1348  goto error;
1349  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1350  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1351  goto error;
1352  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1353  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1354  goto error;
1355  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1356  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1357  goto error;
1358  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1359  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1360 
1361  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1362  goto error;
1363  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1364  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1365  goto error;
1366  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1367  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1368  goto error;
1369  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1370  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1371  goto error;
1372  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1373  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1374 
1375  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1376  goto error;
1377  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1378  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1379  goto error;
1380  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1381  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1382  goto error;
1383  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1384  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1385  goto error;
1386  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1387  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LE);
1388 
1389  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1390  goto error;
1391  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1392  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1393  goto error;
1394  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1395  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1396  goto error;
1397  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1398  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1399  goto error;
1400  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1401  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1402 
1403  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1404  goto error;
1405  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1406  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1407  goto error;
1408  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1409  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1410  goto error;
1411  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1412  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1413  goto error;
1414  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1415  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LE);
1416 
1417  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1418  goto error;
1419  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1420  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1421  goto error;
1422  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1423  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1424  goto error;
1425  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1426  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1427  goto error;
1428  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1429  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LT);
1430 
1431  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1432  goto error;
1433  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1434  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1435  goto error;
1436  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1437  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1438  goto error;
1439  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1440  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1441  goto error;
1442  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1443  /* we could get a LE */
1444  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1445 
1446  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1447  goto error;
1448  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1449  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1450  goto error;
1451  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1452  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1453  goto error;
1454  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1455  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1456  goto error;
1457  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1458  /* we could get a LE */
1459  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1460 
1461  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1462  goto error;
1463  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1464  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1465  goto error;
1466  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1467  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1468  goto error;
1469  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1470  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1471  goto error;
1472  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1473  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1474 
1475  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1476  goto error;
1477  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1478  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1479  goto error;
1480  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1481  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1482  goto error;
1483  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1484  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1485  goto error;
1486  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1487  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1488 
1489  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1490  goto error;
1491  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1492  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1493  goto error;
1494  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1495  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1496  goto error;
1497  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1498  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1499  goto error;
1500  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1501  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1502 
1503  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1504  goto error;
1505  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1506  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1507  goto error;
1508  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1509  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1510  goto error;
1511  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1512  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1513  goto error;
1514  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1515  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1516 
1517  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1518  goto error;
1519  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1520  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1521  goto error;
1522  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1523  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1524  goto error;
1525  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1526  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1527  goto error;
1528  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1529  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1530 
1531  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1532  goto error;
1533  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1534  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1535  goto error;
1536  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1537  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1538  goto error;
1539  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1540  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1541  goto error;
1542  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1543  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1544 
1545  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1546  goto error;
1547  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1548  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1549  goto error;
1550  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1551  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1552  goto error;
1553  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1554  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1555  goto error;
1556  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1557  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1558 
1559  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1560  goto error;
1561  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1562  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1563  goto error;
1564  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1565  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1566  goto error;
1567  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1568  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1569  goto error;
1570  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1571  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1572 
1573  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1574  goto error;
1575  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1576  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1577  goto error;
1578  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1579  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1580  goto error;
1581  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1582  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1583  goto error;
1584  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1585  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1586 
1587  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1588  goto error;
1589  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1590  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1591  goto error;
1592  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1593  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1594  goto error;
1595  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1596  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1597  goto error;
1598  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1599  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GT);
1600 
1601  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1602  goto error;
1603  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1604  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1605  goto error;
1606  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1607  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1608  goto error;
1609  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1610  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1611  goto error;
1612  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1613  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GT);
1614 
1615  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1616  goto error;
1617  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1618  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1619  goto error;
1620  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1621  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1622  goto error;
1623  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1624  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1625  goto error;
1626  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1627  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GT);
1628 
1629  if (a != NULL)
1630  DetectAddressFree(a);
1631  if (b != NULL)
1632  DetectAddressFree(b);
1633  return result;
1634 
1635  error:
1636  if (a != NULL)
1637  DetectAddressFree(a);
1638  if (b != NULL)
1639  DetectAddressFree(b);
1640  return 0;
1641 }
1642 
1643 static int AddressTestIPv6CutNot01(void)
1644 {
1645  DetectAddress *a = NULL;
1646  DetectAddress *b = NULL;
1647  struct in6_addr in6;
1648  int result = 1;
1649 
1650  if ( (a = DetectAddressInit()) == NULL)
1651  goto error;
1652 
1653  if (inet_pton(AF_INET6, "::", &in6) != 1)
1654  goto error;
1655  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1656  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1657  goto error;
1658  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1659  result &= (DetectAddressCutNotIPv6(a, &b) == -1);
1660 
1661  if (a != NULL)
1662  DetectAddressFree(a);
1663  if (b != NULL)
1664  DetectAddressFree(b);
1665  return result;
1666 
1667  error:
1668  if (a != NULL)
1669  DetectAddressFree(a);
1670  if (b != NULL)
1671  DetectAddressFree(b);
1672  return 0;
1673 }
1674 
1675 static int AddressTestIPv6CutNot02(void)
1676 {
1677  DetectAddress *a = NULL;
1678  DetectAddress *b = NULL;
1679  DetectAddress *temp = NULL;
1680  struct in6_addr in6;
1681  int result = 1;
1682 
1683  if ( (a = DetectAddressInit()) == NULL)
1684  goto error;
1685  if ( (temp = DetectAddressInit()) == NULL)
1686  goto error;
1687 
1688  if (inet_pton(AF_INET6, "::", &in6) != 1)
1689  goto error;
1690  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1691  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1692  goto error;
1693  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1694  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1695 
1696  result &= (b == NULL);
1697 
1698  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1699  goto error;
1700  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1701  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1702  goto error;
1703  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1704 
1705  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1706 
1707  if (a != NULL)
1708  DetectAddressFree(a);
1709  if (b != NULL)
1710  DetectAddressFree(b);
1711  if (temp != NULL)
1712  DetectAddressFree(temp);
1713  return result;
1714 
1715  error:
1716  if (a != NULL)
1717  DetectAddressFree(a);
1718  if (b != NULL)
1719  DetectAddressFree(b);
1720  if (temp != NULL)
1721  DetectAddressFree(temp);
1722  return 0;
1723 }
1724 
1725 static int AddressTestIPv6CutNot03(void)
1726 {
1727  DetectAddress *a = NULL;
1728  DetectAddress *b = NULL;
1729  DetectAddress *temp = NULL;
1730  struct in6_addr in6;
1731  int result = 1;
1732 
1733  if ( (a = DetectAddressInit()) == NULL)
1734  goto error;
1735  if ( (temp = DetectAddressInit()) == NULL)
1736  goto error;
1737 
1738  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1739  goto error;
1740  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1741  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1742  goto error;
1743  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1744  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1745 
1746  result &= (b == NULL);
1747 
1748  if (inet_pton(AF_INET6, "::", &in6) != 1)
1749  goto error;
1750  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1751  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1752  goto error;
1753  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1754 
1755  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1756 
1757  if (a != NULL)
1758  DetectAddressFree(a);
1759  if (b != NULL)
1760  DetectAddressFree(b);
1761  if (temp != NULL)
1762  DetectAddressFree(temp);
1763  return result;
1764 
1765  error:
1766  if (a != NULL)
1767  DetectAddressFree(a);
1768  if (b != NULL)
1769  DetectAddressFree(b);
1770  if (temp != NULL)
1771  DetectAddressFree(temp);
1772  return 0;
1773 }
1774 
1775 static int AddressTestIPv6CutNot04(void)
1776 {
1777  DetectAddress *a = NULL;
1778  DetectAddress *b = NULL;
1779  DetectAddress *temp = NULL;
1780  struct in6_addr in6;
1781  int result = 1;
1782 
1783  if ( (a = DetectAddressInit()) == NULL)
1784  goto error;
1785  if ( (temp = DetectAddressInit()) == NULL)
1786  goto error;
1787 
1788  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1789  goto error;
1790  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1791  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1792  goto error;
1793  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1794  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1795 
1796  if (inet_pton(AF_INET6, "::", &in6) != 1)
1797  goto error;
1798  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1799  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1800  goto error;
1801  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1802  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1803 
1804  result &= (b != NULL);
1805  if (result == 0)
1806  goto error;
1807  if (inet_pton(AF_INET6, "2000::2", &in6) != 1)
1808  goto error;
1809  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1810  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1811  goto error;
1812  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1813  result = (DetectAddressCmpIPv6(b, temp) == ADDRESS_EQ);
1814 
1815  if (a != NULL)
1816  DetectAddressFree(a);
1817  if (b != NULL)
1818  DetectAddressFree(b);
1819  if (temp != NULL)
1820  DetectAddressFree(temp);
1821  return result;
1822 
1823  error:
1824  if (a != NULL)
1825  DetectAddressFree(a);
1826  if (b != NULL)
1827  DetectAddressFree(b);
1828  if (temp != NULL)
1829  DetectAddressFree(temp);
1830  return 0;
1831 }
1832 
1833 static int AddressTestIPv6CutNot05(void)
1834 {
1835  DetectAddress *a = NULL;
1836  DetectAddress *b = NULL;
1837  DetectAddress *temp = NULL;
1838  struct in6_addr in6;
1839  int result = 1;
1840 
1841  if ( (a = DetectAddressInit()) == NULL)
1842  goto error;
1843  if ( (temp = DetectAddressInit()) == NULL)
1844  goto error;
1845 
1846  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1847  goto error;
1848  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1849  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1850  goto error;
1851  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1852  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1853 
1854  if (inet_pton(AF_INET6, "::", &in6) != 1)
1855  goto error;
1856  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1857  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1858  goto error;
1859  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1860  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1861 
1862  result &= (b != NULL);
1863  if (result == 0)
1864  goto error;
1865  if (inet_pton(AF_INET6, "2000::21", &in6) != 1)
1866  goto error;
1867  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1868  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1869  goto error;
1870  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1871  result = (DetectAddressCmpIPv6(b, temp) == ADDRESS_EQ);
1872 
1873  if (a != NULL)
1874  DetectAddressFree(a);
1875  if (b != NULL)
1876  DetectAddressFree(b);
1877  if (temp != NULL)
1878  DetectAddressFree(temp);
1879  return result;
1880 
1881  error:
1882  if (a != NULL)
1883  DetectAddressFree(a);
1884  if (b != NULL)
1885  DetectAddressFree(b);
1886  if (temp != NULL)
1887  DetectAddressFree(temp);
1888  return 0;
1889 }
1890 
1891 #endif /* UNITTESTS */
1892 
1894 {
1895 
1896 #ifdef UNITTESTS
1897  UtRegisterTest("AddressTestIPv6Gt01", AddressTestIPv6Gt01);
1898  UtRegisterTest("AddressTestIPv6Gt02", AddressTestIPv6Gt02);
1899  UtRegisterTest("AddressTestIPv6Gt03", AddressTestIPv6Gt03);
1900  UtRegisterTest("AddressTestIPv6Gt04", AddressTestIPv6Gt04);
1901 
1902  UtRegisterTest("AddressTestIPv6Lt01", AddressTestIPv6Lt01);
1903  UtRegisterTest("AddressTestIPv6Lt02", AddressTestIPv6Lt02);
1904  UtRegisterTest("AddressTestIPv6Lt03", AddressTestIPv6Lt03);
1905  UtRegisterTest("AddressTestIPv6Lt04", AddressTestIPv6Lt04);
1906 
1907  UtRegisterTest("AddressTestIPv6Eq01", AddressTestIPv6Eq01);
1908  UtRegisterTest("AddressTestIPv6Eq02", AddressTestIPv6Eq02);
1909  UtRegisterTest("AddressTestIPv6Eq03", AddressTestIPv6Eq03);
1910  UtRegisterTest("AddressTestIPv6Eq04", AddressTestIPv6Eq04);
1911 
1912  UtRegisterTest("AddressTestIPv6Le01", AddressTestIPv6Le01);
1913  UtRegisterTest("AddressTestIPv6Le02", AddressTestIPv6Le02);
1914  UtRegisterTest("AddressTestIPv6Le03", AddressTestIPv6Le03);
1915  UtRegisterTest("AddressTestIPv6Le04", AddressTestIPv6Le04);
1916  UtRegisterTest("AddressTestIPv6Le05", AddressTestIPv6Le05);
1917 
1918  UtRegisterTest("AddressTestIPv6Ge01", AddressTestIPv6Ge01);
1919  UtRegisterTest("AddressTestIPv6Ge02", AddressTestIPv6Ge02);
1920  UtRegisterTest("AddressTestIPv6Ge03", AddressTestIPv6Ge03);
1921  UtRegisterTest("AddressTestIPv6Ge04", AddressTestIPv6Ge04);
1922  UtRegisterTest("AddressTestIPv6Ge05", AddressTestIPv6Ge05);
1923 
1924  UtRegisterTest("AddressTestIPv6SubOne01", AddressTestIPv6SubOne01);
1925  UtRegisterTest("AddressTestIPv6SubOne02", AddressTestIPv6SubOne02);
1926 
1927  UtRegisterTest("AddressTestIPv6AddOne01", AddressTestIPv6AddOne01);
1928  UtRegisterTest("AddressTestIPv6AddOne02", AddressTestIPv6AddOne02);
1929 
1930  UtRegisterTest("AddressTestIPv6AddressCmp01", AddressTestIPv6AddressCmp01);
1931 
1932  UtRegisterTest("AddressTestIPv6CutNot01", AddressTestIPv6CutNot01);
1933  UtRegisterTest("AddressTestIPv6CutNot02", AddressTestIPv6CutNot02);
1934  UtRegisterTest("AddressTestIPv6CutNot03", AddressTestIPv6CutNot03);
1935  UtRegisterTest("AddressTestIPv6CutNot04", AddressTestIPv6CutNot04);
1936  UtRegisterTest("AddressTestIPv6CutNot05", AddressTestIPv6CutNot05);
1937 #endif /* UNITTESTS */
1938 
1939  return;
1940 }
ADDRESS_EB
@ ADDRESS_EB
Definition: detect.h:128
DetectAddress_::ip
Address ip
Definition: detect.h:141
DetectAddressFree
void DetectAddressFree(DetectAddress *ag)
Frees a DetectAddress instance.
Definition: detect-engine-address.c:80
DetectAddressCutIPv6
int DetectAddressCutIPv6(DetectEngineCtx *de_ctx, DetectAddress *a, DetectAddress *b, DetectAddress **c)
Definition: detect-engine-address-ipv6.c:359
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:139
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:335
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:761
Address_
Definition: decode.h:111
util-unittest.h
util-cidr.h
ADDRESS_GT
@ ADDRESS_GT
Definition: detect.h:130
ADDRESS_ER
@ ADDRESS_ER
Definition: detect.h:123
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:16
detect.h
detect-engine-port.h
ADDRESS_GE
@ ADDRESS_GE
Definition: detect.h:129
DetectAddress_::ip2
Address ip2
Definition: detect.h:142
ADDRESS_LT
@ ADDRESS_LT
Definition: detect.h:124
suricata-common.h
AddressIPv6EqU32
int AddressIPv6EqU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:140
Address_::address
union Address_::@42 address
AddressIPv6GeU32
int AddressIPv6GeU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:205
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:374
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:723
ADDRESS_ES
@ ADDRESS_ES
Definition: detect.h:127
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:112
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:1893
AddressIPv6LtU32
int AddressIPv6LtU32(uint32_t *a, uint32_t *b)
Definition: detect-engine-address-ipv6.c:66
ADDRESS_LE
@ ADDRESS_LE
Definition: detect.h:125
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
ADDRESS_EQ
@ ADDRESS_EQ
Definition: detect.h:126
DetectAddressInit
DetectAddress * DetectAddressInit(void)
Creates and returns a new instance of a DetectAddress.
Definition: detect-engine-address.c:67
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