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  * \brief Extends a target address range if the the source address range is
774  * wider than the target address range on either sides.
775  *
776  * Every address is a range, i.e. address->ip1....address->ip2. For
777  * example 2000::-2010::
778  * if source->ip1 is smaller than target->ip1, it indicates that the
779  * source's left address limit is greater(range wise) than the target's
780  * left address limit, and hence we reassign the target's left address
781  * limit to source's left address limit.
782  * Similary if source->ip2 is greater than target->ip2, it indicates that
783  * the source's right address limit is greater(range wise) than the
784  * target's right address limit, and hence we reassign the target's right
785  * address limit to source's right address limit.
786  *
787  * \param de_ctx Pointer to the detection engine context.
788  * \param target Pointer to the target DetectAddress instance that has to be
789  * updated.
790  * \param source Pointer to the source DetectAddress instance that is used
791  * to decided whether we extend the target's address range.
792  *
793  * \retval 0 On success.
794  * \retval -1 On failure.
795  */
797  DetectAddress *source)
798 {
799  if (AddressIPv6Lt(&source->ip, &target->ip)) {
800  COPY_ADDRESS(&source->ip, &target->ip);
801  }
802 
803  if (AddressIPv6Gt(&source->ip, &target->ip)) {
804  COPY_ADDRESS(&source->ip2, &target->ip2);
805  }
806 
807  return 0;
808 }
809 
810 
811 /***************************************Unittests******************************/
812 
813 #ifdef UNITTESTS
814 
815 static int AddressTestIPv6Gt01(void)
816 {
817  int result = 0;
818 
819  uint32_t a[4] = { 1, 2, 3, 4 };
820  uint32_t b[4] = { 0, 2, 3, 4 };
821 
822  if (AddressIPv6GtU32(a, b) == 1)
823  result = 1;
824 
825  return result;
826 }
827 
828 static int AddressTestIPv6Gt02(void)
829 {
830  int result = 0;
831 
832  uint32_t a[4] = { 0, 2, 3, 4 };
833  uint32_t b[4] = { 1, 2, 3, 4 };
834 
835  if (AddressIPv6GtU32(a, b) == 0)
836  result = 1;
837 
838  return result;
839 }
840 
841 static int AddressTestIPv6Gt03(void)
842 {
843  int result = 0;
844 
845  uint32_t a[4] = { 1, 2, 3, 4 };
846  uint32_t b[4] = { 1, 2, 3, 4 };
847 
848  if (AddressIPv6GtU32(a, b) == 0)
849  result = 1;
850 
851  return result;
852 }
853 
854 static int AddressTestIPv6Gt04(void)
855 {
856  int result = 0;
857 
858  uint32_t a[4] = { 1, 2, 3, 5 };
859  uint32_t b[4] = { 1, 2, 3, 4 };
860 
861  if (AddressIPv6GtU32(a, b) == 1)
862  result = 1;
863 
864  return result;
865 }
866 
867 static int AddressTestIPv6Lt01(void)
868 {
869  int result = 0;
870 
871  uint32_t a[4] = { 0, 2, 3, 4 };
872  uint32_t b[4] = { 1, 2, 3, 4 };
873 
874  if (AddressIPv6LtU32(a, b) == 1)
875  result = 1;
876 
877  return result;
878 }
879 
880 static int AddressTestIPv6Lt02(void)
881 {
882  int result = 0;
883 
884  uint32_t a[4] = { 1, 2, 3, 4 };
885  uint32_t b[4] = { 0, 2, 3, 4 };
886 
887  if (AddressIPv6LtU32(a, b) == 0)
888  result = 1;
889 
890  return result;
891 }
892 
893 static int AddressTestIPv6Lt03(void)
894 {
895  int result = 0;
896 
897  uint32_t a[4] = { 1, 2, 3, 4 };
898  uint32_t b[4] = { 1, 2, 3, 4 };
899 
900  if (AddressIPv6LtU32(a, b) == 0)
901  result = 1;
902 
903  return result;
904 }
905 
906 static int AddressTestIPv6Lt04(void)
907 {
908  int result = 0;
909 
910  uint32_t a[4] = { 1, 2, 3, 4 };
911  uint32_t b[4] = { 1, 2, 3, 5 };
912 
913  if (AddressIPv6LtU32(a, b) == 1)
914  result = 1;
915 
916  return result;
917 }
918 
919 static int AddressTestIPv6Eq01(void)
920 {
921  int result = 0;
922 
923  uint32_t a[4] = { 0, 2, 3, 4 };
924  uint32_t b[4] = { 1, 2, 3, 4 };
925 
926  if (AddressIPv6EqU32(a, b) == 0)
927  result = 1;
928 
929  return result;
930 }
931 
932 static int AddressTestIPv6Eq02(void)
933 {
934  int result = 0;
935 
936  uint32_t a[4] = { 1, 2, 3, 4 };
937  uint32_t b[4] = { 0, 2, 3, 4 };
938 
939  if (AddressIPv6EqU32(a, b) == 0)
940  result = 1;
941 
942  return result;
943 }
944 
945 static int AddressTestIPv6Eq03(void)
946 {
947  int result = 0;
948 
949  uint32_t a[4] = { 1, 2, 3, 4 };
950  uint32_t b[4] = { 1, 2, 3, 4 };
951 
952  if (AddressIPv6EqU32(a, b) == 1)
953  result = 1;
954 
955  return result;
956 }
957 
958 static int AddressTestIPv6Eq04(void)
959 {
960  int result = 0;
961 
962  uint32_t a[4] = { 1, 2, 3, 4 };
963  uint32_t b[4] = { 1, 2, 3, 5 };
964 
965  if (AddressIPv6EqU32(a, b) == 0)
966  result = 1;
967 
968  return result;
969 }
970 
971 static int AddressTestIPv6Le01(void)
972 {
973  int result = 0;
974 
975  uint32_t a[4] = { 0, 2, 3, 4 };
976  uint32_t b[4] = { 1, 2, 3, 4 };
977 
978  if (AddressIPv6LeU32(a, b) == 1)
979  result = 1;
980 
981  return result;
982 }
983 
984 static int AddressTestIPv6Le02(void)
985 {
986  int result = 0;
987 
988  uint32_t a[4] = { 1, 2, 3, 4 };
989  uint32_t b[4] = { 0, 2, 3, 4 };
990 
991  if (AddressIPv6LeU32(a, b) == 0)
992  result = 1;
993 
994  return result;
995 }
996 
997 static int AddressTestIPv6Le03(void)
998 {
999  int result = 0;
1000 
1001  uint32_t a[4] = { 1, 2, 3, 4 };
1002  uint32_t b[4] = { 1, 2, 3, 4 };
1003 
1004  if (AddressIPv6LeU32(a, b) == 1)
1005  result = 1;
1006 
1007  return result;
1008 }
1009 
1010 static int AddressTestIPv6Le04(void)
1011 {
1012  int result = 0;
1013 
1014  uint32_t a[4] = { 1, 2, 3, 4 };
1015  uint32_t b[4] = { 1, 2, 3, 5 };
1016 
1017  if (AddressIPv6LeU32(a, b) == 1)
1018  result = 1;
1019 
1020  return result;
1021 }
1022 
1023 static int AddressTestIPv6Le05(void)
1024 {
1025  int result = 0;
1026 
1027  uint32_t a[4];
1028  uint32_t b[4];
1029  struct in6_addr in6;
1030 
1031  if (inet_pton(AF_INET6, "1999:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &in6) != 1)
1032  return 0;
1033  memcpy(&a, &in6.s6_addr, sizeof(in6.s6_addr));
1034 
1035  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1036  return 0;
1037  memcpy(&b, &in6.s6_addr, sizeof(in6.s6_addr));
1038 
1039  if (AddressIPv6LeU32(a, b) == 1)
1040  result = 1;
1041 
1042  return result;
1043 }
1044 
1045 static int AddressTestIPv6Ge01(void)
1046 {
1047  int result = 0;
1048 
1049  uint32_t a[4] = { 0, 2, 3, 4 };
1050  uint32_t b[4] = { 1, 2, 3, 4 };
1051 
1052  if (AddressIPv6GeU32(a, b) == 0)
1053  result = 1;
1054 
1055  return result;
1056 }
1057 
1058 static int AddressTestIPv6Ge02(void)
1059 {
1060  int result = 0;
1061 
1062  uint32_t a[4] = { 1, 2, 3, 4 };
1063  uint32_t b[4] = { 0, 2, 3, 4 };
1064 
1065  if (AddressIPv6GeU32(a, b) == 1)
1066  result = 1;
1067 
1068  return result;
1069 }
1070 
1071 static int AddressTestIPv6Ge03(void)
1072 {
1073  int result = 0;
1074 
1075  uint32_t a[4] = { 1, 2, 3, 4 };
1076  uint32_t b[4] = { 1, 2, 3, 4 };
1077 
1078  if (AddressIPv6GeU32(a, b) == 1)
1079  result = 1;
1080 
1081  return result;
1082 }
1083 
1084 static int AddressTestIPv6Ge04(void)
1085 {
1086  int result = 0;
1087 
1088  uint32_t a[4] = { 1, 2, 3, 4 };
1089  uint32_t b[4] = { 1, 2, 3, 5 };
1090 
1091  if (AddressIPv6GeU32(a, b) == 0)
1092  result = 1;
1093 
1094  return result;
1095 }
1096 
1097 static int AddressTestIPv6Ge05(void)
1098 {
1099  int result = 0;
1100 
1101  uint32_t a[4];
1102  uint32_t b[4];
1103  struct in6_addr in6;
1104 
1105  if (inet_pton(AF_INET6, "1999:ffff:ffff:ffff:ffff:ffff:ffff:ffff", &in6) != 1)
1106  return 0;
1107  memcpy(&a, &in6.s6_addr, sizeof(in6.s6_addr));
1108 
1109  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1110  return 0;
1111  memcpy(&b, &in6.s6_addr, sizeof(in6.s6_addr));
1112 
1113  if (AddressIPv6GeU32(a, b) == 0)
1114  result = 1;
1115 
1116  return result;
1117 }
1118 
1119 static int AddressTestIPv6SubOne01(void)
1120 {
1121  int result = 0;
1122 
1123  uint32_t a[4], b[4];
1124  struct in6_addr in6;
1125 
1126  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1127  return 0;
1128  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1129 
1130  a[0] = SCNtohl(a[0]);
1131  a[1] = SCNtohl(a[1]);
1132  a[2] = SCNtohl(a[2]);
1133  a[3] = SCNtohl(a[3]);
1134 
1135  AddressCutIPv6CopySubOne(a, b);
1136 
1137  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1138  return 0;
1139  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1140  if (b[0] == a[0] && b[1] == a[1] &&
1141  b[2] == a[2] && b[3] == a[3]) {
1142  result = 1;
1143  }
1144 
1145  return result;
1146 }
1147 
1148 static int AddressTestIPv6SubOne02(void)
1149 {
1150  int result = 0;
1151 
1152  uint32_t a[4], b[4];
1153  struct in6_addr in6;
1154 
1155  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1156  return 0;
1157  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1158 
1159  a[0] = SCNtohl(a[0]);
1160  a[1] = SCNtohl(a[1]);
1161  a[2] = SCNtohl(a[2]);
1162  a[3] = SCNtohl(a[3]);
1163 
1164  AddressCutIPv6CopySubOne(a, b);
1165 
1166  if (inet_pton(AF_INET6, "1FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1167  return 0;
1168  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1169  if (b[0] == a[0] && b[1] == a[1] &&
1170  b[2] == a[2] && b[3] == a[3]) {
1171  result = 1;
1172  }
1173 
1174  return result;
1175 }
1176 
1177 static int AddressTestIPv6AddOne01(void)
1178 {
1179  int result = 0;
1180 
1181  uint32_t a[4], b[4];
1182  struct in6_addr in6;
1183 
1184  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1185  return 0;
1186  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1187 
1188  a[0] = SCNtohl(a[0]);
1189  a[1] = SCNtohl(a[1]);
1190  a[2] = SCNtohl(a[2]);
1191  a[3] = SCNtohl(a[3]);
1192 
1193  AddressCutIPv6CopyAddOne(a, b);
1194 
1195  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1196  return 0;
1197  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1198  if (b[0] == a[0] && b[1] == a[1] &&
1199  b[2] == a[2] && b[3] == a[3]) {
1200  result = 1;
1201  }
1202 
1203  return result;
1204 }
1205 
1206 static int AddressTestIPv6AddOne02(void)
1207 {
1208  int result = 0;
1209 
1210  uint32_t a[4], b[4];
1211  struct in6_addr in6;
1212 
1213  if (inet_pton(AF_INET6, "1FFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1214  return 0;
1215  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1216 
1217  a[0] = SCNtohl(a[0]);
1218  a[1] = SCNtohl(a[1]);
1219  a[2] = SCNtohl(a[2]);
1220  a[3] = SCNtohl(a[3]);
1221 
1222  AddressCutIPv6CopyAddOne(a, b);
1223 
1224  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1225  return 0;
1226  memcpy(a, in6.s6_addr, sizeof(in6.s6_addr));
1227  if (b[0] == a[0] && b[1] == a[1] &&
1228  b[2] == a[2] && b[3] == a[3]) {
1229  result = 1;
1230  }
1231 
1232  return result;
1233 }
1234 
1235 static int AddressTestIPv6AddressCmp01(void)
1236 {
1239  struct in6_addr in6;
1240  int result = 1;
1241 
1242  if (a == NULL || b == NULL)
1243  goto error;
1244 
1245  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1246  goto error;
1247  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1248  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1249  goto error;
1250  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1251  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1252  goto error;
1253  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1254  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1255  goto error;
1256  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1257  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EQ);
1258 
1259  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1260  goto error;
1261  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1262  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1263  goto error;
1264  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1265  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1266  goto error;
1267  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1268  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1269  goto error;
1270  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1271  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1272 
1273  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1274  goto error;
1275  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1276  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1277  goto error;
1278  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1279  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1280  goto error;
1281  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1282  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1283  goto error;
1284  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1285  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1286 
1287  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1288  goto error;
1289  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1290  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1291  goto error;
1292  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1293  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1294  goto error;
1295  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1296  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1297  goto error;
1298  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1299  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1300 
1301  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1302  goto error;
1303  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1304  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1305  goto error;
1306  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1307  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1308  goto error;
1309  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1310  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1311  goto error;
1312  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1313  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_ES);
1314 
1315  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1316  goto error;
1317  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1318  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1319  goto error;
1320  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1321  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1322  goto error;
1323  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1324  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1325  goto error;
1326  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1327  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_ES);
1328 
1329  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1330  goto error;
1331  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1332  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1333  goto error;
1334  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1335  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1336  goto error;
1337  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1338  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1339  goto error;
1340  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1341  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1342 
1343  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1344  goto error;
1345  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1346  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1347  goto error;
1348  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1349  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1350  goto error;
1351  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1352  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1353  goto error;
1354  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1355  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1356 
1357  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1358  goto error;
1359  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1360  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1361  goto error;
1362  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1363  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1364  goto error;
1365  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1366  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1367  goto error;
1368  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1369  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_EB);
1370 
1371  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1372  goto error;
1373  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1374  if (inet_pton(AF_INET6, "2000::11", &in6) != 1)
1375  goto error;
1376  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1377  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1378  goto error;
1379  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1380  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1381  goto error;
1382  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1383  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_EB);
1384 
1385  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1386  goto error;
1387  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1388  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1389  goto error;
1390  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1391  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1392  goto error;
1393  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1394  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1395  goto error;
1396  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1397  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1398 
1399  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1400  goto error;
1401  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1402  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1403  goto error;
1404  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1405  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1406  goto error;
1407  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1408  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1409  goto error;
1410  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1411  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1412 
1413  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1414  goto error;
1415  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1416  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1417  goto error;
1418  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1419  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1420  goto error;
1421  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1422  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1423  goto error;
1424  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1425  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LE);
1426 
1427  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1428  goto error;
1429  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1430  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1431  goto error;
1432  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1433  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1434  goto error;
1435  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1436  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1437  goto error;
1438  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1439  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LE);
1440 
1441  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1442  goto error;
1443  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1444  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1445  goto error;
1446  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1447  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1448  goto error;
1449  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1450  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1451  goto error;
1452  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1453  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LE);
1454 
1455  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1456  goto error;
1457  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1458  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1459  goto error;
1460  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1461  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1462  goto error;
1463  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1464  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1465  goto error;
1466  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1467  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_LT);
1468 
1469  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1470  goto error;
1471  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1472  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1473  goto error;
1474  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1475  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1476  goto error;
1477  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1478  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1479  goto error;
1480  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1481  /* we could get a LE */
1482  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1483 
1484  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1485  goto error;
1486  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1487  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1488  goto error;
1489  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1490  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1491  goto error;
1492  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1493  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1494  goto error;
1495  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1496  /* we could get a LE */
1497  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1498 
1499  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1500  goto error;
1501  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1502  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1503  goto error;
1504  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1505  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1506  goto error;
1507  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1508  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1509  goto error;
1510  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1511  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1512 
1513  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1514  goto error;
1515  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1516  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1517  goto error;
1518  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1519  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1520  goto error;
1521  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1522  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1523  goto error;
1524  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1525  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1526 
1527  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1528  goto error;
1529  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1530  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1531  goto error;
1532  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1533  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1534  goto error;
1535  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1536  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1537  goto error;
1538  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1539  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_LT);
1540 
1541  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1542  goto error;
1543  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1544  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1545  goto error;
1546  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1547  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1548  goto error;
1549  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1550  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1551  goto error;
1552  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1553  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1554 
1555  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1556  goto error;
1557  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1558  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1559  goto error;
1560  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1561  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1562  goto error;
1563  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1564  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1565  goto error;
1566  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1567  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1568 
1569  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1570  goto error;
1571  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1572  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1573  goto error;
1574  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1575  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1576  goto error;
1577  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1578  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1579  goto error;
1580  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1581  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1582 
1583  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1584  goto error;
1585  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1586  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1587  goto error;
1588  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1589  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1590  goto error;
1591  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1592  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1593  goto error;
1594  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1595  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GE);
1596 
1597  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1598  goto error;
1599  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1600  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1601  goto error;
1602  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1603  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1604  goto error;
1605  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1606  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1607  goto error;
1608  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1609  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1610 
1611  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1612  goto error;
1613  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1614  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1615  goto error;
1616  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1617  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1618  goto error;
1619  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1620  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1621  goto error;
1622  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1623  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GE);
1624 
1625  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1626  goto error;
1627  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1628  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1629  goto error;
1630  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1631  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1632  goto error;
1633  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1634  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1635  goto error;
1636  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1637  result &= (DetectAddressCmpIPv6(a, b) == ADDRESS_GT);
1638 
1639  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1640  goto error;
1641  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1642  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1643  goto error;
1644  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1645  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1646  goto error;
1647  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1648  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1649  goto error;
1650  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1651  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GT);
1652 
1653  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1654  goto error;
1655  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1656  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1657  goto error;
1658  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1659  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1660  goto error;
1661  memcpy(&b->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1662  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1663  goto error;
1664  memcpy(&b->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1665  result &= (DetectAddressCmpIPv6(a, b) != ADDRESS_GT);
1666 
1667  if (a != NULL)
1668  DetectAddressFree(a);
1669  if (b != NULL)
1670  DetectAddressFree(b);
1671  return result;
1672 
1673  error:
1674  if (a != NULL)
1675  DetectAddressFree(a);
1676  if (b != NULL)
1677  DetectAddressFree(b);
1678  return 0;
1679 }
1680 
1681 static int AddressTestIPv6CutNot01(void)
1682 {
1683  DetectAddress *a = NULL;
1684  DetectAddress *b = NULL;
1685  struct in6_addr in6;
1686  int result = 1;
1687 
1688  if ( (a = DetectAddressInit()) == NULL)
1689  goto error;
1690 
1691  if (inet_pton(AF_INET6, "::", &in6) != 1)
1692  goto error;
1693  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1694  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1695  goto error;
1696  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1697  result &= (DetectAddressCutNotIPv6(a, &b) == -1);
1698 
1699  if (a != NULL)
1700  DetectAddressFree(a);
1701  if (b != NULL)
1702  DetectAddressFree(b);
1703  return result;
1704 
1705  error:
1706  if (a != NULL)
1707  DetectAddressFree(a);
1708  if (b != NULL)
1709  DetectAddressFree(b);
1710  return 0;
1711 }
1712 
1713 static int AddressTestIPv6CutNot02(void)
1714 {
1715  DetectAddress *a = NULL;
1716  DetectAddress *b = NULL;
1717  DetectAddress *temp = NULL;
1718  struct in6_addr in6;
1719  int result = 1;
1720 
1721  if ( (a = DetectAddressInit()) == NULL)
1722  goto error;
1723  if ( (temp = DetectAddressInit()) == NULL)
1724  goto error;
1725 
1726  if (inet_pton(AF_INET6, "::", &in6) != 1)
1727  goto error;
1728  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1729  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1730  goto error;
1731  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1732  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1733 
1734  result &= (b == NULL);
1735 
1736  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1737  goto error;
1738  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1739  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1740  goto error;
1741  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1742 
1743  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1744 
1745  if (a != NULL)
1746  DetectAddressFree(a);
1747  if (b != NULL)
1748  DetectAddressFree(b);
1749  if (temp != NULL)
1750  DetectAddressFree(temp);
1751  return result;
1752 
1753  error:
1754  if (a != NULL)
1755  DetectAddressFree(a);
1756  if (b != NULL)
1757  DetectAddressFree(b);
1758  if (temp != NULL)
1759  DetectAddressFree(temp);
1760  return 0;
1761 }
1762 
1763 static int AddressTestIPv6CutNot03(void)
1764 {
1765  DetectAddress *a = NULL;
1766  DetectAddress *b = NULL;
1767  DetectAddress *temp = NULL;
1768  struct in6_addr in6;
1769  int result = 1;
1770 
1771  if ( (a = DetectAddressInit()) == NULL)
1772  goto error;
1773  if ( (temp = DetectAddressInit()) == NULL)
1774  goto error;
1775 
1776  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1777  goto error;
1778  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1779  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1780  goto error;
1781  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1782  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1783 
1784  result &= (b == NULL);
1785 
1786  if (inet_pton(AF_INET6, "::", &in6) != 1)
1787  goto error;
1788  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1789  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1790  goto error;
1791  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1792 
1793  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1794 
1795  if (a != NULL)
1796  DetectAddressFree(a);
1797  if (b != NULL)
1798  DetectAddressFree(b);
1799  if (temp != NULL)
1800  DetectAddressFree(temp);
1801  return result;
1802 
1803  error:
1804  if (a != NULL)
1805  DetectAddressFree(a);
1806  if (b != NULL)
1807  DetectAddressFree(b);
1808  if (temp != NULL)
1809  DetectAddressFree(temp);
1810  return 0;
1811 }
1812 
1813 static int AddressTestIPv6CutNot04(void)
1814 {
1815  DetectAddress *a = NULL;
1816  DetectAddress *b = NULL;
1817  DetectAddress *temp = NULL;
1818  struct in6_addr in6;
1819  int result = 1;
1820 
1821  if ( (a = DetectAddressInit()) == NULL)
1822  goto error;
1823  if ( (temp = DetectAddressInit()) == NULL)
1824  goto error;
1825 
1826  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1827  goto error;
1828  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1829  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1830  goto error;
1831  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1832  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1833 
1834  if (inet_pton(AF_INET6, "::", &in6) != 1)
1835  goto error;
1836  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1837  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1838  goto error;
1839  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1840  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1841 
1842  result &= (b != NULL);
1843  if (result == 0)
1844  goto error;
1845  if (inet_pton(AF_INET6, "2000::2", &in6) != 1)
1846  goto error;
1847  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1848  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1849  goto error;
1850  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1851  result = (DetectAddressCmpIPv6(b, temp) == ADDRESS_EQ);
1852 
1853  if (a != NULL)
1854  DetectAddressFree(a);
1855  if (b != NULL)
1856  DetectAddressFree(b);
1857  if (temp != NULL)
1858  DetectAddressFree(temp);
1859  return result;
1860 
1861  error:
1862  if (a != NULL)
1863  DetectAddressFree(a);
1864  if (b != NULL)
1865  DetectAddressFree(b);
1866  if (temp != NULL)
1867  DetectAddressFree(temp);
1868  return 0;
1869 }
1870 
1871 static int AddressTestIPv6CutNot05(void)
1872 {
1873  DetectAddress *a = NULL;
1874  DetectAddress *b = NULL;
1875  DetectAddress *temp = NULL;
1876  struct in6_addr in6;
1877  int result = 1;
1878 
1879  if ( (a = DetectAddressInit()) == NULL)
1880  goto error;
1881  if ( (temp = DetectAddressInit()) == NULL)
1882  goto error;
1883 
1884  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1885  goto error;
1886  memcpy(&a->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1887  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1888  goto error;
1889  memcpy(&a->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1890  result &= (DetectAddressCutNotIPv6(a, &b) == 0);
1891 
1892  if (inet_pton(AF_INET6, "::", &in6) != 1)
1893  goto error;
1894  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1895  if (inet_pton(AF_INET6, "2000::0", &in6) != 1)
1896  goto error;
1897  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1898  result = (DetectAddressCmpIPv6(a, temp) == ADDRESS_EQ);
1899 
1900  result &= (b != NULL);
1901  if (result == 0)
1902  goto error;
1903  if (inet_pton(AF_INET6, "2000::21", &in6) != 1)
1904  goto error;
1905  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1906  if (inet_pton(AF_INET6, "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", &in6) != 1)
1907  goto error;
1908  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1909  result = (DetectAddressCmpIPv6(b, temp) == ADDRESS_EQ);
1910 
1911  if (a != NULL)
1912  DetectAddressFree(a);
1913  if (b != NULL)
1914  DetectAddressFree(b);
1915  if (temp != NULL)
1916  DetectAddressFree(temp);
1917  return result;
1918 
1919  error:
1920  if (a != NULL)
1921  DetectAddressFree(a);
1922  if (b != NULL)
1923  DetectAddressFree(b);
1924  if (temp != NULL)
1925  DetectAddressFree(temp);
1926  return 0;
1927 }
1928 
1929 static int AddressTestIPv6Join01(void)
1930 {
1931  DetectAddress *source = DetectAddressInit();
1932  DetectAddress *target = DetectAddressInit();
1933  DetectAddress *temp = DetectAddressInit();
1934  struct in6_addr in6;
1935  int result = 1;
1936 
1937  if (source == NULL || target == NULL || temp == NULL)
1938  goto error;
1939 
1940  /* case 1 */
1941  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1942  goto error;
1943  memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1944  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1945  goto error;
1946  memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1947 
1948  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1949  goto error;
1950  memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1951  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1952  goto error;
1953  memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1954 
1955  result &= (DetectAddressJoinIPv6(NULL, target, source) == 0);
1956  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1957  goto error;
1958  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1959  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1960  goto error;
1961  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1962  result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ);
1963 
1964  /* case 2 */
1965  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1966  goto error;
1967  memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1968  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1969  goto error;
1970  memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1971 
1972  if (inet_pton(AF_INET6, "2000::2", &in6) != 1)
1973  goto error;
1974  memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1975  if (inet_pton(AF_INET6, "2000::19", &in6) != 1)
1976  goto error;
1977  memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1978 
1979  result &= (DetectAddressJoinIPv6(NULL, target, source) == 0);
1980  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1981  goto error;
1982  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1983  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
1984  goto error;
1985  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1986  result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ);
1987 
1988  /* case 3 */
1989  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
1990  goto error;
1991  memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1992  if (inet_pton(AF_INET6, "2000::15", &in6) != 1)
1993  goto error;
1994  memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
1995 
1996  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
1997  goto error;
1998  memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
1999  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2000  goto error;
2001  memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2002 
2003  result &= (DetectAddressJoinIPv6(NULL, target, source) == 0);
2004  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
2005  goto error;
2006  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2007  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2008  goto error;
2009  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2010  result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ);
2011 
2012  /* case 4 */
2013  if (inet_pton(AF_INET6, "2000::10", &in6) != 1)
2014  goto error;
2015  memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2016  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2017  goto error;
2018  memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2019 
2020  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
2021  goto error;
2022  memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2023  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2024  goto error;
2025  memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2026 
2027  result &= (DetectAddressJoinIPv6(NULL, target, source) == 0);
2028  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
2029  goto error;
2030  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2031  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2032  goto error;
2033  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2034  result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ);
2035 
2036  /* case 5 */
2037  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
2038  goto error;
2039  memcpy(&target->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2040  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2041  goto error;
2042  memcpy(&target->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2043 
2044  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
2045  goto error;
2046  memcpy(&source->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2047  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2048  goto error;
2049  memcpy(&source->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2050 
2051  result &= (DetectAddressJoinIPv6(NULL, target, source) == 0);
2052  if (inet_pton(AF_INET6, "2000::1", &in6) != 1)
2053  goto error;
2054  memcpy(&temp->ip.address, in6.s6_addr, sizeof(in6.s6_addr));
2055  if (inet_pton(AF_INET6, "2000::20", &in6) != 1)
2056  goto error;
2057  memcpy(&temp->ip2.address, in6.s6_addr, sizeof(in6.s6_addr));
2058  result = (DetectAddressCmpIPv6(target, temp) == ADDRESS_EQ);
2059 
2060  if (source != NULL)
2061  DetectAddressFree(source);
2062  if (target != NULL)
2063  DetectAddressFree(target);
2064  if (temp != NULL)
2065  DetectAddressFree(temp);
2066  return result;
2067 
2068  error:
2069  if (source != NULL)
2070  DetectAddressFree(source);
2071  if (target != NULL)
2072  DetectAddressFree(target);
2073  if (temp != NULL)
2074  DetectAddressFree(temp);
2075 
2076  return 0;
2077 }
2078 
2079 #endif /* UNITTESTS */
2080 
2082 {
2083 
2084 #ifdef UNITTESTS
2085  UtRegisterTest("AddressTestIPv6Gt01", AddressTestIPv6Gt01);
2086  UtRegisterTest("AddressTestIPv6Gt02", AddressTestIPv6Gt02);
2087  UtRegisterTest("AddressTestIPv6Gt03", AddressTestIPv6Gt03);
2088  UtRegisterTest("AddressTestIPv6Gt04", AddressTestIPv6Gt04);
2089 
2090  UtRegisterTest("AddressTestIPv6Lt01", AddressTestIPv6Lt01);
2091  UtRegisterTest("AddressTestIPv6Lt02", AddressTestIPv6Lt02);
2092  UtRegisterTest("AddressTestIPv6Lt03", AddressTestIPv6Lt03);
2093  UtRegisterTest("AddressTestIPv6Lt04", AddressTestIPv6Lt04);
2094 
2095  UtRegisterTest("AddressTestIPv6Eq01", AddressTestIPv6Eq01);
2096  UtRegisterTest("AddressTestIPv6Eq02", AddressTestIPv6Eq02);
2097  UtRegisterTest("AddressTestIPv6Eq03", AddressTestIPv6Eq03);
2098  UtRegisterTest("AddressTestIPv6Eq04", AddressTestIPv6Eq04);
2099 
2100  UtRegisterTest("AddressTestIPv6Le01", AddressTestIPv6Le01);
2101  UtRegisterTest("AddressTestIPv6Le02", AddressTestIPv6Le02);
2102  UtRegisterTest("AddressTestIPv6Le03", AddressTestIPv6Le03);
2103  UtRegisterTest("AddressTestIPv6Le04", AddressTestIPv6Le04);
2104  UtRegisterTest("AddressTestIPv6Le05", AddressTestIPv6Le05);
2105 
2106  UtRegisterTest("AddressTestIPv6Ge01", AddressTestIPv6Ge01);
2107  UtRegisterTest("AddressTestIPv6Ge02", AddressTestIPv6Ge02);
2108  UtRegisterTest("AddressTestIPv6Ge03", AddressTestIPv6Ge03);
2109  UtRegisterTest("AddressTestIPv6Ge04", AddressTestIPv6Ge04);
2110  UtRegisterTest("AddressTestIPv6Ge05", AddressTestIPv6Ge05);
2111 
2112  UtRegisterTest("AddressTestIPv6SubOne01", AddressTestIPv6SubOne01);
2113  UtRegisterTest("AddressTestIPv6SubOne02", AddressTestIPv6SubOne02);
2114 
2115  UtRegisterTest("AddressTestIPv6AddOne01", AddressTestIPv6AddOne01);
2116  UtRegisterTest("AddressTestIPv6AddOne02", AddressTestIPv6AddOne02);
2117 
2118  UtRegisterTest("AddressTestIPv6AddressCmp01", AddressTestIPv6AddressCmp01);
2119 
2120  UtRegisterTest("AddressTestIPv6CutNot01", AddressTestIPv6CutNot01);
2121  UtRegisterTest("AddressTestIPv6CutNot02", AddressTestIPv6CutNot02);
2122  UtRegisterTest("AddressTestIPv6CutNot03", AddressTestIPv6CutNot03);
2123  UtRegisterTest("AddressTestIPv6CutNot04", AddressTestIPv6CutNot04);
2124  UtRegisterTest("AddressTestIPv6CutNot05", AddressTestIPv6CutNot05);
2125 
2126  UtRegisterTest("AddressTestIPv6Join01", AddressTestIPv6Join01);
2127 #endif /* UNITTESTS */
2128 
2129  return;
2130 }
int AddressIPv6LeU32(uint32_t *a, uint32_t *b)
#define COPY_ADDRESS(a, b)
Definition: decode.h:124
#define SCLogDebug(...)
Definition: util-debug.h:335
int AddressIPv6EqU32(uint32_t *a, uint32_t *b)
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 ...
Address ip
Definition: detect.h:145
address structure for use in the detection engine.
Definition: detect.h:143
int AddressIPv6LtU32(uint32_t *a, uint32_t *b)
union Address_::@48 address
main detection engine ctx
Definition: detect.h:720
char family
Definition: decode.h:110
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Address ip2
Definition: detect.h:146
void DetectAddressFree(DetectAddress *ag)
Frees a DetectAddress instance.
#define SCNtohl(x)
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...
int AddressIPv6GtU32(uint32_t *a, uint32_t *b)
int DetectAddressCmpIPv6(DetectAddress *a, DetectAddress *b)
Compares 2 addresses(address ranges) and returns the relationship between the 2 addresses.
void DetectAddressIPv6Tests(void)
DetectAddress * DetectAddressInit(void)
Creates and returns a new instance of a DetectAddress.
int AddressIPv6Eq(Address *a, Address *b)
Compares 2 ipv6 addresses and returns if the addresses are equal or not.
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...
int DetectAddressJoinIPv6(DetectEngineCtx *de_ctx, DetectAddress *target, DetectAddress *source)
Extends a target address range if the the source address range is wider than the target address range...
int DetectAddressCutIPv6(DetectEngineCtx *de_ctx, DetectAddress *a, DetectAddress *b, DetectAddress **c)
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...
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...
int AddressIPv6GeU32(uint32_t *a, uint32_t *b)