/
icmpresponder.py
411 lines (355 loc) · 19.7 KB
/
icmpresponder.py
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import itertools
import os
import vlcp.service.sdn.ofpportmanager as ofpportmanager
import vlcp.service.kvdb.objectdb as objectdb
import vlcp.service.sdn.ioprocessing as iop
from vlcp.service.sdn.flowbase import FlowBase
from vlcp.server.module import depend, call_api
from vlcp.config.config import defaultconfig
from vlcp.event.runnable import RoutineContainer
from vlcp.service.sdn.ofpmanager import FlowInitialize
from vlcp.utils.ethernet import mac_addr_bytes, ip4_addr_bytes,ip4_icmp_payload,\
ethernet_l7, ip4_packet_l7, ip4_payload,ICMP_ECHOREPLY,icmp_bestparse,icmp_echo,\
ip_frag
from vlcp.utils.flowupdater import FlowUpdater
from vlcp.protocol.openflow.openflow import OpenflowConnectionStateEvent, OpenflowAsyncMessageEvent
from vlcp.utils.networkmodel import SubNet,RouterPort
from namedstruct.stdprim import uint16
from vlcp.event.event import M_
class ICMPResponderUpdater(FlowUpdater):
def __init__(self,connection,parent):
super(ICMPResponderUpdater,self).__init__(connection,(),('icmpresponderupdate',connection),parent._logger)
self.parent = parent
self._lastlognets = ()
self._lastlogports = ()
self._lastsubnetsinfo = dict()
self._orig_initialkeys = ()
async def main(self):
try:
self.subroutine(self._update_handler(),True,"update_handler_routine")
# use controller to reply icmp ping ,so start routine handler packet in
if not self.parent.prepush:
self.subroutine(self._icmp_packetin_handler(),True,"icmp_packetin_handler_routine")
await FlowUpdater.main(self)
finally:
if hasattr(self,"update_handler_routine"):
self.update_handler_routine.close()
if hasattr(self,"icmp_packetin_handler_routine"):
self.icmp_packetin_handler_routine.close()
async def _icmp_packetin_handler(self):
conn = self._connection
ofdef = self._connection.openflowdef
l3input = self.parent._gettableindex("l3input",self._connection.protocol.vhost)
transactid = uint16.create(os.urandom(2))
async def send_packet_out(portid,packet):
await self.execute_commands(conn,
[
ofdef.ofp_packet_out(
buffer_id = ofdef.OFP_NO_BUFFER,
in_port = ofdef.OFPP_CONTROLLER,
actions = [
ofdef.ofp_action_output(port = portid,
max_len = ofdef.OFPCML_NO_BUFFER
)
],
data = packet._tobytes()
)
])
icmp_packetin_matcher = OpenflowAsyncMessageEvent.createMatcher(ofdef.OFPT_PACKET_IN,None,None,l3input,2,
self._connection,self._connection.connmark)
while True:
ev = await icmp_packetin_matcher
msg = ev.message
inport = ofdef.ofp_port_no.create(ofdef.get_oxm(msg.match.oxm_fields,ofdef.OXM_OF_IN_PORT))
# it must be icmp packet ...
icmp_packet = ethernet_l7.create(msg.data)
if ip_frag(icmp_packet) != 0:
# ignore fragmented packets
continue
transactid = (transactid + 1) & 0xffff
reply_packet = ip4_packet_l7((ip4_payload,ip4_icmp_payload),
(icmp_bestparse, icmp_echo),
dl_src = icmp_packet.dl_dst,
dl_dst = icmp_packet.dl_src,
ip_src = icmp_packet.ip_dst,
ip_dst = icmp_packet.ip_src,
frag_off = 0,
ttl = 128,
identifier = transactid,
icmp_type = ICMP_ECHOREPLY,
icmp_code = icmp_packet.icmp_code,
icmp_id = icmp_packet.icmp_id,
icmp_seq = icmp_packet.icmp_seq,
data = icmp_packet.data
)
self.subroutine(send_packet_out(inport,reply_packet))
async def _update_handler(self):
# when lgport,lgnet,phyport,phynet object change , receive this event from ioprocessing module
dataobjectchange = iop.DataObjectChanged.createMatcher(None,None,self._connection)
while True:
ev = await dataobjectchange
# save to instance attr , us in other method
self._lastlogports,_,self._lastlognets,_ = ev.current
self._update_walk()
def _walk_lgport(self,key,value,walk,save):
if value is not None:
save(key)
if hasattr(value,'subnet'):
try:
subnetobj = walk(value.subnet.getkey())
except KeyError:
pass
else:
save(value.subnet.getkey())
if subnetobj is not None and hasattr(subnetobj,"router"):
try:
_ = walk(subnetobj.router.getkey())
except KeyError:
pass
else:
save(subnetobj.router.getkey())
def _walk_lgnet(self,key,value,walk,save):
save(key)
# if value is None, also save its key
# means watch key, when created , we will recv event
def _update_walk(self):
lgportkeys = [p.getkey() for p,_ in self._lastlogports]
lgnetkeys = [p.getkey() for p,_ in self._lastlognets]
self._initialkeys = lgportkeys + lgnetkeys
self._orig_initialkeys = lgportkeys + lgnetkeys
self._walkerdict = dict(itertools.chain(((p,self._walk_lgport) for p in lgportkeys),
((n,self._walk_lgnet) for n in lgnetkeys)))
self.subroutine(self.restart_walk(),False)
def reset_initialkeys(self,keys,values):
# walk map logicalport --> subnet ---> routerport
# we get subnet object, add keys to initialkeys,
# when subnet update, it will restart walk ,, after we will get new routerport
subnetkeys = [k for k,v in zip(keys,values) if v is not None and not v.isdeleted() and
v.isinstance(SubNet)]
self._initialkeys = tuple(itertools.chain(self._orig_initialkeys,subnetkeys))
async def updateflow(self, connection, addvalues, removevalues, updatedvalues):
try:
allobjects = set(o for o in self._savedresult if o is not None and not o.isdeleted())
lastsubnetsinfo = self._lastsubnetsinfo
currentlognetsinfo = dict((n,id) for n,id in self._lastlognets if n in allobjects)
currentrouterportsinfo = dict((o.subnet,o) for o in allobjects
if o.isinstance(RouterPort))
currentsubnetsinfo = dict((o,(getattr(currentrouterportsinfo[o],"ip_address",getattr(o,"gateway",None)),
self.parent.inroutermac,o.network.id,currentlognetsinfo[o.network]))
for o in allobjects if o.isinstance(SubNet)
and hasattr(o,"router") and o in currentrouterportsinfo
and o.network in currentlognetsinfo
and (hasattr(currentrouterportsinfo[o],"ip_address")
or hasattr(o,"gateway"))
and ( not hasattr(o,"isexternal") or o.isexternal == False))
self._lastsubnetsinfo = currentsubnetsinfo
ofdef = connection.openflowdef
vhost = connection.protocol.vhost
l3input = self.parent._gettableindex("l3input",vhost)
cmds = []
if connection.protocol.disablenxext:
def match_network(nid):
return ofdef.create_oxm(ofdef.OXM_OF_METADATA_W, (nid & 0xffff) << 32,
b'\x00\x00\xff\xff\x00\x00\x00\x00')
else:
def match_network(nid):
return ofdef.create_oxm(ofdef.NXM_NX_REG4, nid)
# prepush or not ,, it is same , so ..
def _deleteicmpflows(ipaddress, macaddress, networkid):
return [
ofdef.ofp_flow_mod(
cookie = 0x2,
cookie_mask = 0xffffffffffffffff,
table_id = l3input,
command = ofdef.OFPFC_DELETE,
priority = ofdef.OFP_DEFAULT_PRIORITY + 1,
buffer_id = ofdef.OFP_NO_BUFFER,
out_port = ofdef.OFPP_ANY,
out_group = ofdef.OFPG_ANY,
match = ofdef.ofp_match_oxm(
oxm_fields = [
ofdef.create_oxm(ofdef.NXM_NX_REG4,networkid),
ofdef.create_oxm(ofdef.OXM_OF_ETH_DST,mac_addr_bytes(macaddress)),
ofdef.create_oxm(ofdef.OXM_OF_ETH_TYPE,ofdef.ETHERTYPE_IP),
ofdef.create_oxm(ofdef.OXM_OF_IPV4_DST,ip4_addr_bytes(ipaddress)),
ofdef.create_oxm(ofdef.OXM_OF_IP_PROTO,ofdef.IPPROTO_ICMP),
ofdef.create_oxm(ofdef.OXM_OF_ICMPV4_TYPE,8),
ofdef.create_oxm(ofdef.OXM_OF_ICMPV4_CODE,0)
]
)
)
]
if not self.parent.prepush:
def _createicmpflows(ipaddress, macaddress, networkid):
return [
ofdef.ofp_flow_mod(
cookie = 0x2,
cookie_mask = 0xffffffffffffffff,
table_id = l3input,
command = ofdef.OFPFC_ADD,
# icmp to router matcher same as ip forward to router
# so priority + 1
priority = ofdef.OFP_DEFAULT_PRIORITY + 1,
buffer_id = ofdef.OFP_NO_BUFFER,
out_port = ofdef.OFPP_ANY,
out_group = ofdef.OFPG_ANY,
match = ofdef.ofp_match_oxm(
oxm_fields = [
match_network(networkid),
ofdef.create_oxm(ofdef.OXM_OF_ETH_DST,mac_addr_bytes(macaddress)),
ofdef.create_oxm(ofdef.OXM_OF_ETH_TYPE,ofdef.ETHERTYPE_IP),
ofdef.create_oxm(ofdef.OXM_OF_IPV4_DST,ip4_addr_bytes(ipaddress)),
ofdef.create_oxm(ofdef.OXM_OF_IP_PROTO,ofdef.IPPROTO_ICMP),
ofdef.create_oxm(ofdef.OXM_OF_ICMPV4_TYPE,8),
ofdef.create_oxm(ofdef.OXM_OF_ICMPV4_CODE,0)
]
),
instructions = [
ofdef.ofp_instruction_actions(
actions = [
ofdef.ofp_action_output(
port = ofdef.OFPP_CONTROLLER,
max_len = ofdef.OFPCML_NO_BUFFER
)
]
)
]
)
]
else:
def _createicmpflows(ipaddress, macaddress, networkid):
return [
ofdef.ofp_flow_mod(
cookie = 0x2,
cookie_mask = 0xffffffffffffffff,
table_id = l3input,
command = ofdef.OFPFC_ADD,
# icmp to router matcher same as ip forward to router
# so priority + 1
priority = ofdef.OFP_DEFAULT_PRIORITY + 1,
buffer_id = ofdef.OFP_NO_BUFFER,
out_port = ofdef.OFPP_ANY,
out_group = ofdef.OFPG_ANY,
match = ofdef.ofp_match_oxm(
oxm_fields = [
match_network(networkid),
ofdef.create_oxm(ofdef.OXM_OF_ETH_DST,mac_addr_bytes(macaddress)),
ofdef.create_oxm(ofdef.OXM_OF_ETH_TYPE,ofdef.ETHERTYPE_IP),
ofdef.create_oxm(ofdef.OXM_OF_IPV4_DST,ip4_addr_bytes(ipaddress)),
ofdef.create_oxm(ofdef.OXM_OF_IP_PROTO,ofdef.IPPROTO_ICMP),
ofdef.create_oxm(ofdef.OXM_OF_ICMPV4_TYPE,8),
ofdef.create_oxm(ofdef.OXM_OF_ICMPV4_CODE,0)
]
),
instructions = [
ofdef.ofp_instruction_actions(
actions = [
ofdef.nx_action_reg_move(
n_bits = 48,
src = ofdef.OXM_OF_ETH_SRC,
dst = ofdef.OXM_OF_ETH_DST
),
ofdef.ofp_action_set_field(
field = ofdef.create_oxm(
ofdef.OXM_OF_ETH_SRC,
ofdef.mac_addr(macaddress)
)
),
ofdef.nx_action_reg_move(
n_bits = 32,
src = ofdef.OXM_OF_IPV4_SRC,
dst = ofdef.OXM_OF_IPV4_DST
),
ofdef.ofp_action_set_field(
field = ofdef.create_oxm(
ofdef.OXM_OF_IPV4_SRC,
ofdef.ip4_addr(ipaddress)
)
),
ofdef.ofp_action_set_field(
field = ofdef.create_oxm(
ofdef.OXM_OF_ICMPV4_TYPE,
ICMP_ECHOREPLY
)
),
ofdef.ofp_action_nw_ttl(
nw_ttl = 128
),
ofdef.ofp_action_output(
port = ofdef.OFPP_IN_PORT
)
]
)
]
)
]
for subnet in lastsubnetsinfo.keys():
if subnet not in currentsubnetsinfo\
or (subnet in currentsubnetsinfo and lastsubnetsinfo[subnet] != currentsubnetsinfo[subnet]):
# subnet remove or subnet info changed , remove flow info
ip_address, mac_address, networkid, nid = lastsubnetsinfo[subnet]
remove_arp = {(ip_address,mac_address,networkid,True),}
await call_api(self, 'arpresponder', 'removeproxyarp', {'connection':connection,
'arpentries': remove_arp})
cmds.extend(_deleteicmpflows(ip_address,mac_address,nid))
await self.execute_commands(connection, cmds)
for subnet in currentsubnetsinfo.keys():
if subnet not in lastsubnetsinfo\
or (subnet in lastsubnetsinfo and lastsubnetsinfo[subnet] != currentsubnetsinfo[subnet]):
ip_address, mac_address, networkid, nid = currentsubnetsinfo[subnet]
add_arp = {(ip_address,mac_address,networkid,True),}
await call_api(self, 'arpresponder', 'createproxyarp', {'connection': connection,
'arpentries': add_arp})
cmds.extend(_createicmpflows(ip_address,mac_address,nid))
await self.execute_commands(connection, cmds)
except Exception:
self._logger.warning("Unexpected exception in icmp_flow_updater, ignore it! Continue",exc_info=True)
@defaultconfig
@depend(ofpportmanager.OpenflowPortManager,objectdb.ObjectDB)
class ICMPResponder(FlowBase):
"""
Respond ICMP echo (ping) requests to the gateway
"""
_tablerequest = (
("l3input",("l2input",),""),
("l2output",("l3input",),"")
)
# True : reply icmp ping with flow
# False: reply icmp ping with controller PACKET_IN/PACKET_OUT
#
# Must use prepush=True with OpenvSwitch 2.5+
#
_default_prepush = False
# "Gateway" responds with this MAC address
_default_inroutermac = '1a:23:67:59:63:33'
def __init__(self,server):
super(ICMPResponder,self).__init__(server)
self.app_routine = RoutineContainer(self.scheduler)
self.app_routine.main = self._main
self.routines.append(self.app_routine)
self._flowupdater = dict()
async def _main(self):
flowinit = FlowInitialize.createMatcher(_ismatch=lambda x: self.vhostbind is None or
x.vhost in self.vhostbind)
conndown = OpenflowConnectionStateEvent.createMatcher(state = OpenflowConnectionStateEvent.CONNECTION_DOWN,
_ismatch=lambda x:self.vhostbind is None or
x.createby.vhost in self.vhostbind)
while True:
ev, m = await M_(flowinit,conndown)
if m is flowinit:
c = ev.connection
self.app_routine.subroutine(self._init_conn(c))
if m is conndown:
c = ev.connection
self.app_routine.subroutine(self._remove_conn(c))
async def _init_conn(self,conn):
if conn in self._flowupdater:
updater = self._flowupdater.pop(conn)
updater.close()
updater = ICMPResponderUpdater(conn,self)
self._flowupdater[conn] = updater
updater.start()
async def _remove_conn(self,conn):
if conn in self._flowupdater:
updater = self._flowupdater.pop(conn)
updater.close()