Revision 5e173e9f filter/filter.c

View differences:

filter/filter.c
90 90
  buffer_puts(buf, "=]");
91 91
}
92 92

  
93
static inline int
94
uint_cmp(uint i1, uint i2)
95
{
96
  return (int)(i1 > i2) - (int)(i1 < i2);
97
}
98

  
99
static inline int
100
u64_cmp(u64 i1, u64 i2)
101
{
102
  return (int)(i1 > i2) - (int)(i1 < i2);
103
}
93
static inline int val_is_ip4(const struct f_val v)
94
{ return (v.type == T_IP) && ipa_is_ip4(v.val.ip); }
104 95

  
105 96
/**
106 97
 * val_compare - compare two values
......
114 105
int
115 106
val_compare(struct f_val v1, struct f_val v2)
116 107
{
117
  int rc;
118

  
119 108
  if (v1.type != v2.type) {
120 109
    if (v1.type == T_VOID)	/* Hack for else */
121 110
      return -1;
......
124 113

  
125 114
#ifndef IPV6
126 115
    /* IP->Quad implicit conversion */
127
    if ((v1.type == T_QUAD) && (v2.type == T_IP))
128
      return uint_cmp(v1.val.i, ipa_to_u32(v2.val.px.ip));
129
    if ((v1.type == T_IP) && (v2.type == T_QUAD))
130
      return uint_cmp(ipa_to_u32(v1.val.px.ip), v2.val.i);
116
    if ((v1.type == T_QUAD) && val_is_ip4(v2))
117
      return uint_cmp(v1.val.i, ipa_to_u32(v2.val.ip));
118
    if (val_is_ip4(v1) && (v2.type == T_QUAD))
119
      return uint_cmp(ipa_to_u32(v1.val.ip), v2.val.i);
131 120
#endif
132 121

  
133 122
    debug( "Types do not match in val_compare\n" );
......
146 135
  case T_EC:
147 136
    return u64_cmp(v1.val.ec, v2.val.ec);
148 137
  case T_IP:
149
    return ipa_compare(v1.val.px.ip, v2.val.px.ip);
150
  case T_PREFIX:
151
    if (rc = ipa_compare(v1.val.px.ip, v2.val.px.ip))
152
      return rc;
153
    return uint_cmp(v1.val.px.len, v2.val.px.len);
138
    return ipa_compare(v1.val.ip, v2.val.ip);
139
  case T_NET:
140
    return net_compare(v1.val.net, v2.val.net);
154 141
  case T_STRING:
155 142
    return strcmp(v1.val.s, v2.val.s);
156 143
  default:
......
209 196
  }
210 197
}
211 198

  
212
void
213
fprefix_get_bounds(struct f_prefix *px, int *l, int *h)
214
{
215
  *l = *h = px->len & LEN_MASK;
216

  
217
  if (px->len & LEN_MINUS)
218
    *l = 0;
219

  
220
  else if (px->len & LEN_PLUS)
221
    *h = ipa_is_ip4(px->ip) ? IP4_MAX_PREFIX_LENGTH : IP6_MAX_PREFIX_LENGTH;
222

  
223
  else if (px->len & LEN_RANGE)
224
    {
225
      *l = 0xff & (px->len >> 16);
226
      *h = 0xff & (px->len >> 8);
227
    }
228
}
229

  
230 199
static int
231 200
clist_set_type(struct f_tree *set, struct f_val *v)
232 201
{
......
385 354
    return int_set_contains(v2.val.ad, v1.val.i);
386 355
#ifndef IPV6
387 356
  /* IP->Quad implicit conversion */
388
  if ((v1.type == T_IP) && (v2.type == T_CLIST))
389
    return int_set_contains(v2.val.ad, ipa_to_u32(v1.val.px.ip));
357
  if (val_is_ip4(v1) && (v2.type == T_CLIST))
358
    return int_set_contains(v2.val.ad, ipa_to_u32(v1.val.ip));
390 359
#endif
391 360

  
392 361
  if ((v1.type == T_EC) && (v2.type == T_ECLIST))
......
395 364
  if ((v1.type == T_STRING) && (v2.type == T_STRING))
396 365
    return patmatch(v2.val.s, v1.val.s);
397 366

  
398
  if ((v1.type == T_IP) && (v2.type == T_PREFIX))
399
    return ipa_in_net(v1.val.px.ip, v2.val.px.ip, v2.val.px.len);
367
  if ((v1.type == T_IP) && (v2.type == T_NET))
368
    return ipa_in_netX(v1.val.ip, v2.val.net);
400 369

  
401
  if ((v1.type == T_PREFIX) && (v2.type == T_PREFIX))
402
    return net_in_net(v1.val.px.ip, v1.val.px.len, v2.val.px.ip, v2.val.px.len);
370
  if ((v1.type == T_NET) && (v2.type == T_NET))
371
    return net_in_netX(v1.val.net, v2.val.net);
403 372

  
404
  if ((v1.type == T_PREFIX) && (v2.type == T_PREFIX_SET))
405
    return trie_match_fprefix(v2.val.ti, &v1.val.px);
373
  if ((v1.type == T_NET) && (v2.type == T_PREFIX_SET))
374
    return trie_match_net(v2.val.ti, v1.val.net);
406 375

  
407 376
  if (v2.type != T_SET)
408 377
    return CMP_ERROR;
......
437 406
  case T_BOOL:	buffer_puts(buf, v.val.i ? "TRUE" : "FALSE"); return;
438 407
  case T_INT:	buffer_print(buf, "%u", v.val.i); return;
439 408
  case T_STRING: buffer_print(buf, "%s", v.val.s); return;
440
  case T_IP:	buffer_print(buf, "%I", v.val.px.ip); return;
441
  case T_PREFIX: buffer_print(buf, "%I/%d", v.val.px.ip, v.val.px.len); return;
409
  case T_IP:	buffer_print(buf, "%I", v.val.ip); return;
410
  case T_NET:   buffer_print(buf, "%N", v.val.net); return;
442 411
  case T_PAIR:	buffer_print(buf, "(%u,%u)", v.val.i >> 16, v.val.i & 0xffff); return;
443 412
  case T_QUAD:	buffer_print(buf, "%R", v.val.i); return;
444 413
  case T_EC:	ec_format(buf2, v.val.ec); buffer_print(buf, "%s", buf2); return;
......
630 599
      }
631 600
#ifndef IPV6
632 601
      /* IP->Quad implicit conversion */
633
      else if (v1.type == T_IP) {
634
	ipv4_used = 1; key = ipa_to_u32(v1.val.px.ip);
602
      else if (val_is_ip4(v1)) {
603
	ipv4_used = 1; key = ipa_to_u32(v1.val.ip);
635 604
      }
636 605
#endif
637 606
      else
......
715 684
    if ((sym->class != (SYM_VARIABLE | v2.type)) && (v2.type != T_VOID)) {
716 685
#ifndef IPV6
717 686
      /* IP->Quad implicit conversion */
718
      if ((sym->class == (SYM_VARIABLE | T_QUAD)) && (v2.type == T_IP)) {
687
      if ((sym->class == (SYM_VARIABLE | T_QUAD)) && val_is_ip4(v2))
688
      {
719 689
	vp->type = T_QUAD;
720
	vp->val.i = ipa_to_u32(v2.val.px.ip);
690
	vp->val.i = ipa_to_u32(v2.val.ip);
721 691
	break;
722 692
      }
723 693
#endif
......
790 760

  
791 761
      switch (what->a2.i)
792 762
      {
793
      case SA_FROM:	res.val.px.ip = rta->from; break;
794
      case SA_GW:	res.val.px.ip = rta->gw; break;
795
      case SA_NET:	res.val.px.ip = net_prefix((*f_rte)->net->n.addr);
796
			res.val.px.len = net_pxlen((*f_rte)->net->n.addr); break;
763
      case SA_FROM:	res.val.ip = rta->from; break;
764
      case SA_GW:	res.val.ip = rta->gw; break;
765
      case SA_NET:	res.val.net = (*f_rte)->net->n.addr; break;
797 766
      case SA_PROTO:	res.val.s = rta->src->proto->name; break;
798 767
      case SA_SOURCE:	res.val.i = rta->source; break;
799 768
      case SA_SCOPE:	res.val.i = rta->scope; break;
......
820 789
      switch (what->a2.i)
821 790
      {
822 791
      case SA_FROM:
823
	rta->from = v1.val.px.ip;
792
	rta->from = v1.val.ip;
824 793
	break;
825 794

  
826 795
      case SA_GW:
827 796
	{
828
	  ip_addr ip = v1.val.px.ip;
797
	  ip_addr ip = v1.val.ip;
829 798
	  neighbor *n = neigh_find(rta->src->proto, &ip, 0);
830 799
	  if (!n || (n->scope == SCOPE_HOST))
831 800
	    runtime( "Invalid gw address" );
......
908 877
      case EAF_TYPE_IP_ADDRESS:
909 878
	res.type = T_IP;
910 879
	struct adata * ad = e->u.ptr;
911
	res.val.px.ip = * (ip_addr *) ad->data;
880
	res.val.ip = * (ip_addr *) ad->data;
912 881
	break;
913 882
      case EAF_TYPE_AS_PATH:
914 883
        res.type = T_PATH;
......
958 927
      case EAF_TYPE_ROUTER_ID:
959 928
#ifndef IPV6
960 929
	/* IP->Quad implicit conversion */
961
	if (v1.type == T_IP) {
962
	  l->attrs[0].u.data = ipa_to_u32(v1.val.px.ip);
930
	if (val_is_ip4(v1)) {
931
	  l->attrs[0].u.data = ipa_to_u32(v1.val.ip);
963 932
	  break;
964 933
	}
965 934
#endif
......
978 947
	int len = sizeof(ip_addr);
979 948
	struct adata *ad = lp_alloc(f_pool, sizeof(struct adata) + len);
980 949
	ad->length = len;
981
	(* (ip_addr *) ad->data) = v1.val.px.ip;
950
	(* (ip_addr *) ad->data) = v1.val.ip;
982 951
	l->attrs[0].u.ptr = ad;
983 952
	break;
984 953
      case EAF_TYPE_AS_PATH:
......
1053 1022
    ONEARG;
1054 1023
    res.type = T_INT;
1055 1024
    switch(v1.type) {
1056
    case T_PREFIX: res.val.i = v1.val.px.len; break;
1025
    case T_NET:    res.val.i = net_pxlen(v1.val.net); break;
1057 1026
    case T_PATH:   res.val.i = as_path_getlen(v1.val.ad); break;
1058 1027
    case T_CLIST:  res.val.i = int_set_get_size(v1.val.ad); break;
1059 1028
    case T_ECLIST: res.val.i = ec_set_get_size(v1.val.ad); break;
......
1062 1031
    break;
1063 1032
  case P('c','p'):	/* Convert prefix to ... */
1064 1033
    ONEARG;
1065
    if (v1.type != T_PREFIX)
1034
    if (v1.type != T_NET)
1066 1035
      runtime( "Prefix expected" );
1067
    res.type = what->aux;
1068
    switch(res.type) {
1069
      /*    case T_INT:	res.val.i = v1.val.px.len; break; Not needed any more */
1070
    case T_IP: res.val.px.ip = v1.val.px.ip; break;
1071
    default: bug( "Unknown prefix to conversion" );
1072
    }
1036
    res.type = T_IP;
1037
    res.val.ip = net_prefix(v1.val.net);
1073 1038
    break;
1074 1039
  case P('a','f'):	/* Get first ASN from AS PATH */
1075 1040
    ONEARG;
......
1135 1100
    {
1136 1101
      ip_addr mask = ipa_mkmask(v2.val.i);
1137 1102
      res.type = T_IP;
1138
      res.val.px.ip = ipa_and(mask, v1.val.px.ip);
1103
      res.val.ip = ipa_and(mask, v1.val.ip);
1139 1104
    }
1140 1105
    break;
1141 1106

  
......
1195 1160
#ifndef IPV6
1196 1161
      /* IP->Quad implicit conversion */
1197 1162
      else if (v2.type == T_IP)
1198
	n = ipa_to_u32(v2.val.px.ip);
1163
	n = ipa_to_u32(v2.val.ip);
1199 1164
#endif
1200 1165
      else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
1201 1166
	arg_set = 1;
......
1284 1249
    if (what->arg1)
1285 1250
    {
1286 1251
      TWOARGS;
1287
      if ((v1.type != T_PREFIX) || (v2.type != T_INT))
1252
      if ((v1.type != T_NET) || (v2.type != T_INT))
1288 1253
	runtime("Invalid argument to roa_check()");
1289 1254

  
1290 1255
      as = v2.val.i;
......
1292 1257
    else
1293 1258
    {
1294 1259
      ACCESS_RTE;
1295
      v1.val.px.ip = net_prefix((*f_rte)->net->n.addr);
1296
      v1.val.px.len = net_pxlen((*f_rte)->net->n.addr);
1260
      v1.val.net = (*f_rte)->net->n.addr;
1297 1261

  
1298 1262
      /* We ignore temporary attributes, probably not a problem here */
1299 1263
      /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
......
1310 1274
      runtime("Missing ROA table");
1311 1275

  
1312 1276
    res.type = T_ENUM_ROA;
1313
    res.val.i = roa_check(rtc->table, v1.val.px.ip, v1.val.px.len, as);
1277
    res.val.i = ROA_UNKNOWN;
1278
    // XXXX res.val.i = roa_check_net(rtc->table, &v1.val.net, as);
1314 1279
    break;
1315 1280

  
1316 1281
  default:

Also available in: Unified diff