Statistics
| Branch: | Tag: | Revision:

root / flowspec / models.py @ 8914c9d9

History | View | Annotate | Download (14.4 kB)

1
#
2
# -*- coding: utf-8 -*- vim:fileencoding=utf-8:
3
# vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
4
#Copyright © 2011-2013 Greek Research and Technology Network (GRNET S.A.)
5

    
6
#Developed by Leonidas Poulopoulos (leopoul-at-noc-dot-grnet-dot-gr),
7
#GRNET NOC
8
#
9
#Permission to use, copy, modify, and/or distribute this software for any
10
#purpose with or without fee is hereby granted, provided that the above
11
#copyright notice and this permission notice appear in all copies.
12
#
13
#THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
14
#TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
15
#FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
16
#CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
17
#DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
18
#ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
19
#SOFTWARE.
20
#
21

    
22

    
23

    
24
from django.db import models
25
from django.conf import settings
26
from django.contrib.auth.models import User
27
from django.utils.translation import ugettext_lazy as _
28
from utils import proxy as PR
29
from ipaddr import *
30
import datetime
31
import logging
32
from time import sleep
33

    
34
import beanstalkc
35
from flowspy.utils.randomizer import id_generator as id_gen
36

    
37
from flowspec.tasks import *
38

    
39
FORMAT = '%(asctime)s %(levelname)s: %(message)s'
40
logging.basicConfig(format=FORMAT)
41
logger = logging.getLogger(__name__)
42
logger.setLevel(logging.DEBUG)
43

    
44

    
45
FRAGMENT_CODES = (
46
    ("dont-fragment", "Don't fragment"),
47
    ("first-fragment", "First fragment"),
48
    ("is-fragment", "Is fragment"),
49
    ("last-fragment", "Last fragment"),
50
    ("not-a-fragment", "Not a fragment")
51
)
52

    
53
THEN_CHOICES = (
54
    ("accept", "Accept"),
55
    ("discard", "Discard"),
56
    ("community", "Community"),
57
    ("next-term", "Next term"),
58
    ("routing-instance", "Routing Instance"),
59
    ("rate-limit", "Rate limit"),
60
    ("sample", "Sample")                
61
)
62

    
63
MATCH_PROTOCOL = (
64
    ("ah", "ah"),
65
    ("egp", "egp"),
66
    ("esp", "esp"),
67
    ("gre", "gre"),
68
    ("icmp", "icmp"),
69
    ("icmp6", "icmp6"),
70
    ("igmp", "igmp"),
71
    ("ipip", "ipip"),
72
    ("ospf", "ospf"),
73
    ("pim", "pim"),
74
    ("rsvp", "rsvp"),
75
    ("sctp", "sctp"),
76
    ("tcp", "tcp"),
77
    ("udp", "udp"),
78
)
79

    
80
ROUTE_STATES = (
81
    ("ACTIVE", "ACTIVE"),
82
    ("ERROR", "ERROR"),
83
    ("EXPIRED", "EXPIRED"),
84
    ("PENDING", "PENDING"),
85
    ("OUTOFSYNC", "OUTOFSYNC"),
86
    ("INACTIVE", "INACTIVE"),
87
    ("ADMININACTIVE", "ADMININACTIVE"),           
88
)
89

    
90

    
91
def days_offset(): return datetime.date.today() + datetime.timedelta(days = settings.EXPIRATION_DAYS_OFFSET)
92
    
93
class MatchPort(models.Model):
94
    port = models.CharField(max_length=24, unique=True)
95
    def __unicode__(self):
96
        return self.port
97
    class Meta:
98
        db_table = u'match_port'    
99

    
100
class MatchDscp(models.Model):
101
    dscp = models.CharField(max_length=24)
102
    def __unicode__(self):
103
        return self.dscp
104
    class Meta:
105
        db_table = u'match_dscp'
106

    
107
class MatchProtocol(models.Model):
108
    protocol = models.CharField(max_length=24, unique=True)
109
    def __unicode__(self):
110
        return self.protocol
111
    class Meta:
112
        db_table = u'match_protocol'
113

    
114
   
115
class ThenAction(models.Model):
116
    action = models.CharField(max_length=60, choices=THEN_CHOICES, verbose_name="Action")
117
    action_value = models.CharField(max_length=255, blank=True, null=True, verbose_name="Action Value")
118
    def __unicode__(self):
119
        ret = "%s:%s" %(self.action, self.action_value)
120
        return ret.rstrip(":")
121
    class Meta:
122
        db_table = u'then_action'
123
        ordering = ['action', 'action_value']
124
        unique_together = ("action", "action_value")
125

    
126
class Route(models.Model):
127
    name = models.SlugField(max_length=128, verbose_name=_("Name"))
128
    applier = models.ForeignKey(User, blank=True, null=True)
129
    source = models.CharField(max_length=32, help_text=_("Network address. Use address/CIDR notation"), verbose_name=_("Source Address"))
130
    sourceport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort", verbose_name=_("Source Port"))
131
    destination = models.CharField(max_length=32, help_text=_("Network address. Use address/CIDR notation"), verbose_name=_("Destination Address"))
132
    destinationport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchDestinationPort", verbose_name=_("Destination Port"))
133
    port = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort", verbose_name=_("Port"))
134
    dscp = models.ManyToManyField(MatchDscp, blank=True, null=True, verbose_name="DSCP")
135
    fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES, blank=True, null=True, verbose_name="Fragment Type")
136
    icmpcode = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Code")
137
    icmptype = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Type")
138
    packetlength = models.IntegerField(blank=True, null=True, verbose_name="Packet Length")
139
    protocol = models.ManyToManyField(MatchProtocol, blank=True, null=True, verbose_name=_("Protocol"))
140
    tcpflag = models.CharField(max_length=128, blank=True, null=True, verbose_name="TCP flag")
141
    then = models.ManyToManyField(ThenAction, verbose_name=_("Then"))
142
    filed = models.DateTimeField(auto_now_add=True)
143
    last_updated = models.DateTimeField(auto_now=True)
144
    status = models.CharField(max_length=20, choices=ROUTE_STATES, blank=True, null=True, verbose_name=_("Status"), default="PENDING")
145
#    is_online = models.BooleanField(default=False)
146
#    is_active = models.BooleanField(default=False)
147
    expires = models.DateField(default=days_offset, verbose_name=_("Expires"))
148
    response = models.CharField(max_length=512, blank=True, null=True, verbose_name=_("Response"))
149
    comments = models.TextField(null=True, blank=True, verbose_name=_("Comments"))
150

    
151
    
152
    def __unicode__(self):
153
        return self.name
154
    
155
    class Meta:
156
        db_table = u'route'
157
        verbose_name = "Rule"
158
        verbose_name_plural = "Rules"
159
    
160
    def save(self, *args, **kwargs):
161
        if not self.pk:
162
            hash = id_gen()
163
            self.name = "%s_%s" %(self.name, hash)
164
        super(Route, self).save(*args, **kwargs) # Call the "real" save() method.
165

    
166
        
167
    def clean(self, *args, **kwargs):
168
        from django.core.exceptions import ValidationError
169
        if self.destination:
170
            try:
171
                address = IPNetwork(self.destination)
172
                self.destination = address.exploded
173
            except Exception:
174
                raise ValidationError(_('Invalid network address format at Destination Field'))
175
        if self.source:
176
            try:
177
                address = IPNetwork(self.source)
178
                self.source = address.exploded
179
            except Exception:
180
                raise ValidationError(_('Invalid network address format at Source Field'))
181
   
182
    def commit_add(self, *args, **kwargs):
183
        peer = self.applier.get_profile().peer.domain_name
184
        send_message("[%s] Adding rule %s. Please wait..." %(self.applier.username, self.name), peer)
185
        response = add.delay(self)
186
        logger.info("Got add job id: %s" %response)
187
        
188
    def commit_edit(self, *args, **kwargs):
189
        peer = self.applier.get_profile().peer.domain_name
190
        send_message("[%s] Editing rule %s. Please wait..." %(self.applier.username, self.name), peer)
191
        response = edit.delay(self)
192
        logger.info("Got edit job id: %s" %response)
193

    
194
    def commit_delete(self, *args, **kwargs):
195
        reason_text = ''
196
        reason = ''
197
        if "reason" in kwargs:
198
            reason = kwargs['reason']
199
            reason_text = "Reason: %s. " %reason
200
        peer = self.applier.get_profile().peer.domain_name
201
        send_message("[%s] Suspending rule %s. %sPlease wait..." %(self.applier.username, self.name, reason_text), peer)
202
        response = delete.delay(self, reason=reason)
203
        logger.info("Got delete job id: %s" %response)
204

    
205
    def has_expired(self):
206
        today = datetime.date.today()
207
        if today > self.expires:
208
            return True
209
        return False
210
    
211
    def check_sync(self):
212
        if not self.is_synced():
213
            self.status = "OUTOFSYNC"
214
            self.save()
215
    
216
    def is_synced(self):
217
        found = False
218
        get_device = PR.Retriever()
219
        device = get_device.fetch_device()
220
        try:
221
            routes = device.routing_options[0].routes
222
        except Exception as e:
223
            self.status = "EXPIRED"
224
            self.save()
225
            logger.error("No routing options on device. Exception: %s" %e)
226
            return True
227
        for route in routes:
228
            if route.name == self.name:
229
                found = True
230
                logger.info('Found a matching rule name')
231
                devicematch = route.match
232
                try:
233
                    assert(self.destination)
234
                    assert(devicematch['destination'][0])
235
                    if self.destination == devicematch['destination'][0]:
236
                        found = found and True
237
                        logger.info('Found a matching destination')
238
                    else:
239
                        found = False
240
                        logger.info('Destination fields do not match')
241
                except:
242
                    pass
243
                try:
244
                    assert(self.source)
245
                    assert(devicematch['source'][0])
246
                    if self.source == devicematch['source'][0]:
247
                        found = found and True
248
                        logger.info('Found a matching source')
249
                    else:
250
                        found = False
251
                        logger.info('Source fields do not match')
252
                except:
253
                    pass
254
                try:
255
                    assert(self.fragmenttype)
256
                    assert(devicematch['fragment'][0])
257
                    if self.fragmenttype == devicematch['fragment'][0]:
258
                        found = found and True
259
                        logger.info('Found a matching fragment type')
260
                    else:
261
                        found = False
262
                        logger.info('Fragment type fields do not match')
263
                except:
264
                    pass
265
                try:
266
                    assert(self.icmpcode)
267
                    assert(devicematch['icmp-code'][0])
268
                    if self.icmpcode == devicematch['icmp-code'][0]:
269
                        found = found and True
270
                        logger.info('Found a matching icmp code')
271
                    else:
272
                        found = False
273
                        logger.info('Icmp code fields do not match')
274
                except:
275
                    pass
276
                try:
277
                    assert(self.icmptype)
278
                    assert(devicematch['icmp-type'][0])
279
                    if self.icmptype == devicematch['icmp-type'][0]:
280
                        found = found and True
281
                        logger.info('Found a matching icmp type')
282
                    else:
283
                        found = False
284
                        logger.info('Icmp type fields do not match')
285
                except:
286
                    pass
287
                if found and self.status != "ACTIVE":
288
                    logger.error('Rule is applied on device but appears as offline')
289
                    self.status = "ACTIVE"
290
                    self.save()
291
                    found = True
292
            if self.status == "ADMININACTIVE" or self.status == "INACTIVE" or self.status == "EXPIRED":
293
                found = True
294
        return found
295

    
296
    def get_then(self):
297
        ret = ''
298
        then_statements = self.then.all()
299
        for statement in then_statements:
300
            if statement.action_value:
301
                ret = "%s %s:<strong>%s</strong><br/>" %(ret, statement.action, statement.action_value)
302
            else: 
303
                ret = "%s %s<br>" %(ret, statement.action)
304
        return ret.rstrip(',')
305
    
306
    get_then.short_description = 'Then statement'
307
    get_then.allow_tags = True
308
#
309
    def get_match(self):
310
        ret = ''
311
        if self.destination:
312
            ret = '%s Dst Addr:<strong>%s</strong> <br/>' %(ret, self.destination)
313
        if self.fragmenttype:
314
            ret = "%s Fragment Type:<strong>%s</strong><br/>" %(ret, self.fragmenttype)
315
        if self.icmpcode:
316
            ret = "%s ICMP code:<strong>%s</strong><br/>" %(ret, self.icmpcode)
317
        if self.icmptype:
318
            ret = "%s ICMP Type:<strong>%s</strong><br/>" %(ret, self.icmptype)
319
        if self.packetlength:
320
            ret = "%s Packet Length:<strong>%s</strong><br/>" %(ret, self.packetlength)
321
        if self.source:
322
            ret = "%s Src Addr:<strong>%s</strong> <br/>" %(ret, self.source)
323
        if self.tcpflag:
324
            ret = "%s TCP flag:<strong>%s</strong><br/>" %(ret, self.tcpflag)
325
        if self.port:
326
            for port in self.port.all():
327
                    ret = ret + "Port:<strong>%s</strong> <br/>" %(port)
328
        if self.protocol:
329
            for protocol in self.protocol.all():
330
                    ret = ret + "Protocol:<strong>%s</strong> <br/>" %(protocol)
331
        if self.destinationport:
332
            for port in self.destinationport.all():
333
                    ret = ret + "Dst Port:<strong>%s</strong> <br/>" %(port)
334
        if self.sourceport:
335
            for port in self.sourceport.all():
336
                    ret = ret +"Src Port:<strong>%s</strong> <br/>" %(port)
337
        if self.dscp:
338
            for dscp in self.dscp.all():
339
                    ret = ret + "%s Port:<strong>%s</strong> <br/>" %(ret, dscp)
340
        return ret.rstrip('<br/>')
341
        
342
    get_match.short_description = 'Match statement'
343
    get_match.allow_tags = True
344
    
345
    @property
346
    def applier_peer(self):
347
        try:
348
            applier_peer = self.applier.get_profile().peer
349
        except:
350
            applier_peer = None
351
        return applier_peer
352
    
353
    @property
354
    def days_to_expire(self):
355
        if self.status not in ['EXPIRED', 'ADMININACTIVE', 'ERROR', 'INACTIVE']:
356
            expiration_days = (self.expires - datetime.date.today()).days
357
            if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
358
                return "%s" %expiration_days
359
            else:
360
                return False
361
        else:
362
            return False
363

    
364
def send_message(msg, user):
365
#    username = user.username
366
    peer = user
367
    b = beanstalkc.Connection()
368
    b.use(settings.POLLS_TUBE)
369
    tube_message = json.dumps({'message': str(msg), 'username':peer})
370
    b.put(tube_message)
371
    b.close()