1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*******************************************************************************
* Copyright (c) 2017 Politecnico di Torino and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution, and is available at
* http://www.apache.org/licenses/LICENSE-2.0
*******************************************************************************/
package mcnet.components;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.microsoft.z3.BoolExpr;
import com.microsoft.z3.Context;
import com.microsoft.z3.DatatypeExpr;
import com.microsoft.z3.Expr;
import com.microsoft.z3.IntExpr;
import com.microsoft.z3.Solver;
import com.microsoft.z3.Z3Exception;
/**Model for a network, encompasses routing and wiring
*
*/
public class Network extends Core{
Context ctx;
NetContext nctx;
List<BoolExpr> constraints;
List<NetworkObject> elements;
public Network(Context ctx,Object[]... args) {
super(ctx, args);
}
@Override
protected void init(Context ctx, Object[]... args) {
this.ctx = ctx;
this.nctx = (NetContext) args[0][0];
constraints = new ArrayList<BoolExpr>();
elements = new ArrayList<NetworkObject>();
}
/**Composes the network linking the configured network objects
*
* @param elements
*/
public void attach (NetworkObject ... elements){
for(NetworkObject el : elements)
this.elements.add(el);
}
@Override
protected void addConstraints(Solver solver) {
try {
BoolExpr[] constr = new BoolExpr[constraints.size()];
solver.add(constraints.toArray(constr));
} catch (Z3Exception e) {
e.printStackTrace();
}
}
/**
* Specify host to address mapping.
* Handles the case in which we have more than one address for a node
* @param addrmap
*/
public void setAddressMappings(ArrayList<Tuple<NetworkObject,ArrayList<DatatypeExpr>>> addrmap){
// Set address mapping for nodes.
for (Tuple<NetworkObject,ArrayList<DatatypeExpr>> entry : addrmap) {
NetworkObject node=entry._1;
List<DatatypeExpr> addr=entry._2;
Expr a_0 = ctx.mkConst(node+"_address_mapping_a_0",nctx.address);
ArrayList<BoolExpr> or_clause = new ArrayList<BoolExpr>();
// Constraint 1 addrToNode(foreach ad in addr) = node
for (DatatypeExpr ad : addr){
constraints.add(ctx.mkEq(nctx.addrToNode.apply(ad), node.getZ3Node()));
or_clause.add(ctx.mkEq(a_0,ad));
}
BoolExpr[] orClause = new BoolExpr[or_clause.size()];
// Constraint 2 nodeHasAddr(node, a_0) == Or(foreach ad in addr (a_0 == ad))
// Note we need the iff here to make sure that we set nodeHasAddr to false
// for other addresses.
constraints.add(ctx.mkForall(new Expr[]{a_0},
ctx.mkEq(ctx.mkOr(or_clause.toArray(orClause)), nctx.nodeHasAddr.apply(node.getZ3Node(), a_0)),1,null,null,null,null));
}
}
/**
* Don't forward packets addressed to node
* @param node
*/
public void saneSend(NetworkObject node){
Expr n_0 = ctx.mkConst(node+"_saneSend_n_0", nctx.node);
Expr p_0 = ctx.mkConst(node+"_saneSend_p_0", nctx.packet);
IntExpr t_0 = ctx.mkIntConst(node+"_saneSend_t_0");
// Constant: node
//Constraint send(node, n_0, p, t_0) -> !nodeHasAddr(node, p.dest)
constraints.add(
ctx.mkForall(new Expr[]{n_0, p_0, t_0},
ctx.mkImplies((BoolExpr)nctx.send.apply(node.getZ3Node(),n_0, p_0, t_0),
ctx.mkNot((BoolExpr)nctx.nodeHasAddr.apply( node.getZ3Node(),
nctx.pf.get("dest").apply(p_0)))),1,null,null,null,null));
}
/**
* Node sends all traffic through gateway
* @param node
* @param gateway
*/
public void setGateway (NetworkObject node, NetworkObject gateway){
// SetGateway(self, node, gateway): All packets from node are sent through gateway
Expr n_0 = ctx.mkConst(node+"_gateway_n_0", nctx.node);
Expr p_0 = ctx.mkConst(node+"_gateway_p_0", nctx.packet);
IntExpr t_0 = ctx.mkIntConst(node+"_gateway_t_0");
//Constraint send(node, n_0, p_0, t_0) -> n_0 = gateway
constraints.add(
ctx.mkForall(new Expr[]{n_0, p_0, t_0},
ctx.mkImplies(
(BoolExpr)nctx.send.apply(node.getZ3Node(), n_0, p_0, t_0),
ctx.mkEq(n_0,gateway.getZ3Node())),1,null,null,null,null));
// constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
// ctx.mkImplies((BoolExpr)nctx.recv.apply(n_0, node.getZ3Node(), p_0, t_0),
// ctx.mkEq(n_0,gateway.getZ3Node())),1,null,null,null,null));
}
/**
* Assigns a specific routing table to a network object. Routing entries in the form: address -> node
* @param node
* @param routing_table
*/
public void routingTable (NetworkObject node,ArrayList<Tuple<DatatypeExpr,NetworkObject>> routing_table){
compositionPolicy(node,routing_table);
}
/**
* Composition policies steer packets between middleboxes.
* @param node
* @param policy
*/
public void compositionPolicy (NetworkObject node,ArrayList<Tuple<DatatypeExpr,NetworkObject>> policy){
//Policy is of the form predicate -> node
Expr p_0 = ctx.mkConst(node+"_composition_p_0", nctx.packet);
Expr n_0 = ctx.mkConst(node+"_composition_n_0", nctx.node);
Expr t_0 = ctx.mkIntConst(node+"_composition_t_0");
HashMap<String,ArrayList<BoolExpr>> collected = new HashMap<String,ArrayList<BoolExpr>>();
HashMap<String,NetworkObject> node_dict = new HashMap<String,NetworkObject>();
BoolExpr predicates;
for(int y=0;y<policy.size();y++){
Tuple<DatatypeExpr,NetworkObject> tp = policy.get(y);
if(collected.containsKey(""+tp._2)) collected.get(""+tp._2).add(nctx.destAddrPredicate(p_0,tp._1));
else{
ArrayList<BoolExpr> alb = new ArrayList<BoolExpr>();
alb.add(nctx.destAddrPredicate(p_0,tp._1));
collected.put(""+tp._2,alb);
}
node_dict.put(""+tp._2, tp._2);
}
//Constraint foreach rtAddr,rtNode in rt( send(node, n_0, p_0, t_0) &&
// Or(foreach rtAddr in rt destAddrPredicate(p_0,rtAddr)) -> n_0 == rtNode )
for (Map.Entry<String,ArrayList<BoolExpr>> entry : collected.entrySet()) {
BoolExpr[] pred = new BoolExpr[entry.getValue().size()];
predicates = ctx.mkOr(entry.getValue().toArray(pred));
constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
ctx.mkImplies(ctx.mkAnd((BoolExpr)nctx.send.apply(node.getZ3Node(), n_0, p_0, t_0), predicates),
ctx.mkEq(n_0, node_dict.get(entry.getKey()).getZ3Node())),1,null,null,null,null));
}
}
/**
* Routing entries are in the form: address -> node. Also allows packet to be sent to another box for further processing
* @param node
* @param routing_table
* @param shunt_node
*/
public void routingTableShunt (NetworkObject node,ArrayList<Tuple<DatatypeExpr,NetworkObject>> routing_table,NetworkObject shunt_node){
compositionPolicyShunt(node,routing_table,shunt_node);
}
/**
* Composition policies steer packets between middleboxes.Policy is in the form: predicate -> node
* @param node
* @param routing_table
* @param shunt_node
*/
public void compositionPolicyShunt (NetworkObject node,ArrayList<Tuple<DatatypeExpr,NetworkObject>> routing_table,NetworkObject shunt_node){
Expr p_0 = ctx.mkConst(node+"_composition_p_0", nctx.packet);
Expr n_0 = ctx.mkConst(node+"_composition_n_0", nctx.node);
Expr t_0 = ctx.mkIntConst(node+"_composition_t_0");
HashMap<String,ArrayList<BoolExpr>> collected = new HashMap<String,ArrayList<BoolExpr>>();
HashMap<String,NetworkObject> node_dict = new HashMap<String,NetworkObject>();
BoolExpr predicates;
for(int y=0;y<routing_table.size();y++){
Tuple<DatatypeExpr,NetworkObject> tp = routing_table.get(y);
if(collected.containsKey(""+tp._2)) collected.get(""+tp._2).add(nctx.destAddrPredicate(p_0,tp._1));
else{
ArrayList<BoolExpr> alb = new ArrayList<BoolExpr>();
alb.add(nctx.destAddrPredicate(p_0,tp._1));
collected.put(""+tp._2,alb);
}
node_dict.put(""+tp._2, tp._2);
}
//Constraint foreach rtAddr,rtNode in rt( send(node, n_0, p_0, t_0) &&
// Or(foreach rtAddr in rt destAddrPredicate(p_0,rtAddr)) -> n_0 == rtNode )
for (Map.Entry<String,ArrayList<BoolExpr>> entry : collected.entrySet()) {
BoolExpr[] pred = new BoolExpr[entry.getValue().size()];
predicates = ctx.mkOr(entry.getValue().toArray(pred));
constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
ctx.mkImplies(ctx.mkAnd((BoolExpr)nctx.send.apply(node.getZ3Node(), n_0, p_0, t_0), predicates),
ctx.mkOr(ctx.mkEq(n_0, node_dict.get(entry.getKey()).getZ3Node()),ctx.mkEq(n_0, shunt_node.getZ3Node()))),1,null,null,null,null));
}
}
// public void SimpleIsolation (NetworkObject node, ArrayList<DatatypeExpr> addresses){
// Expr p = ctx.mkConst(node+"_s_p", nctx.packet);
// Expr n = ctx.mkConst(node+"_s_n", nctx.node);
// IntExpr t = ctx.mkInt(node+"_s_t");
//
// BoolExpr[] a_pred= new BoolExpr[addresses.size()];
// for(int y=0;y<addresses.size();y++){
// DatatypeExpr de = addresses.get(y);
// a_pred[y] = ctx.mkOr(ctx.mkEq(nctx.pf.get("src").apply(p), de),ctx.mkEq(nctx.pf.get("dest").apply(p), de));
// }
//
// constraints.add(
// ctx.mkForall(new Expr[]{p, n, t},
// ctx.mkImplies((BoolExpr)nctx.recv.apply(n, node.getZ3Node(), p, t),
// ctx.mkOr(a_pred)),1,null,null,null,null));
// constraints.add(
// ctx.mkForall(new Expr[]{p, n, t},
// ctx.mkImplies((BoolExpr)nctx.send.apply(node.getZ3Node(), n, p, t),
// ctx.mkOr(a_pred)),1,null,null,null,null));
// }
/**
* Set isolation constraints on a node.
* Doesn't need to be set but useful when interfering policies are in play.
* @param node
* @param adjacencies
*
*/
public void SetIsolationConstraint ( NetworkObject node, ArrayList<NetworkObject> adjacencies){
Expr n_0 = ctx.mkConst(node+"_isolation_n_0", nctx.node);
Expr p_0 = ctx.mkConst(node+"_isolation_p_0", nctx.packet);
IntExpr t_0 = ctx.mkInt(node+"_isolation_t_0");
BoolExpr[] adj = new BoolExpr[adjacencies.size()];
for(int y=0;y<adjacencies.size();y++){
NetworkObject no = adjacencies.get(y);
adj[y] = ctx.mkEq(n_0,no.getZ3Node());
}
BoolExpr clause = ctx.mkOr(adj);
constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
ctx.mkImplies((BoolExpr)nctx.send.apply(node.getZ3Node(), n_0, p_0, t_0),
clause),1,null,null,null,null));
constraints.add(ctx.mkForall(new Expr[]{n_0, p_0, t_0},
ctx.mkImplies((BoolExpr)nctx.recv.apply(n_0, node.getZ3Node(), p_0, t_0),
clause),1,null,null,null,null));
}
/**
* Return all currently attached endhosts
* @return NetworkObject
*/
public List<String> EndHosts(){
List<String> att_nos = new ArrayList<String>();
for(NetworkObject el :elements){
if(el.isEndHost){
System.out.println("el: "+el);
att_nos.add(el.getZ3Node().toString());
}
}
return att_nos;
}
}
|