1 # -*- coding: utf-8 -*- vim:encoding=utf-8:
2 # vim: tabstop=4:shiftwidth=4:softtabstop=4:expandtab
4 from django.db import models
5 from django.conf import settings
6 from django.contrib.auth.models import User
7 from utils import proxy as PR
11 from flowspec.tasks import *
12 from time import sleep
15 from flowspy.utils.randomizer import id_generator as id_gen
18 FORMAT = '%(asctime)s %(levelname)s: %(message)s'
19 logging.basicConfig(format=FORMAT)
20 logger = logging.getLogger(__name__)
21 logger.setLevel(logging.DEBUG)
25 ("dont-fragment", "Don't fragment"),
26 ("first-fragment", "First fragment"),
27 ("is-fragment", "Is fragment"),
28 ("last-fragment", "Last fragment"),
29 ("not-a-fragment", "Not a fragment")
34 ("discard", "Discard"),
35 ("community", "Community"),
36 ("next-term", "Next term"),
37 ("routing-instance", "Routing Instance"),
38 ("rate-limit", "Rate limit"),
45 ("EXPIRED", "EXPIRED"),
46 ("PENDING", "PENDING"),
47 ("OUTOFSYNC", "OUTOFSYNC"),
48 ("INACTIVE", "INACTIVE"),
49 ("ADMININACTIVE", "ADMININACTIVE"),
53 def days_offset(): return datetime.date.today() + datetime.timedelta(days = settings.EXPIRATION_DAYS_OFFSET)
55 class MatchPort(models.Model):
56 port = models.CharField(max_length=24, unique=True)
57 def __unicode__(self):
60 db_table = u'match_port'
62 class MatchDscp(models.Model):
63 dscp = models.CharField(max_length=24)
64 def __unicode__(self):
67 db_table = u'match_dscp'
70 class ThenAction(models.Model):
71 action = models.CharField(max_length=60, choices=THEN_CHOICES, verbose_name="Action")
72 action_value = models.CharField(max_length=255, blank=True, null=True, verbose_name="Action Value")
73 def __unicode__(self):
74 ret = "%s:%s" %(self.action, self.action_value)
75 return ret.rstrip(":")
77 db_table = u'then_action'
78 ordering = ['action', 'action_value']
79 unique_together = ("action", "action_value")
81 class Route(models.Model):
82 name = models.SlugField(max_length=128)
83 applier = models.ForeignKey(User, blank=True, null=True)
84 source = models.CharField(max_length=32, help_text=u"Network address. Use address/CIDR notation", verbose_name="Source Address")
85 sourceport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchSourcePort", verbose_name="Source Port")
86 destination = models.CharField(max_length=32, help_text=u"Network address. Use address/CIDR notation", verbose_name="Destination Address")
87 destinationport = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchDestinationPort", verbose_name="Destination Port")
88 port = models.ManyToManyField(MatchPort, blank=True, null=True, related_name="matchPort", verbose_name="Port" )
89 dscp = models.ManyToManyField(MatchDscp, blank=True, null=True, verbose_name="DSCP")
90 fragmenttype = models.CharField(max_length=20, choices=FRAGMENT_CODES, blank=True, null=True, verbose_name="Fragment Type")
91 icmpcode = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Code")
92 icmptype = models.CharField(max_length=32, blank=True, null=True, verbose_name="ICMP Type")
93 packetlength = models.IntegerField(blank=True, null=True, verbose_name="Packet Length")
94 protocol = models.CharField(max_length=32, blank=True, null=True, verbose_name="Protocol")
95 tcpflag = models.CharField(max_length=128, blank=True, null=True, verbose_name="TCP flag")
96 then = models.ManyToManyField(ThenAction, verbose_name="Then")
97 filed = models.DateTimeField(auto_now_add=True)
98 last_updated = models.DateTimeField(auto_now=True)
99 status = models.CharField(max_length=20, choices=ROUTE_STATES, blank=True, null=True, verbose_name="Status", default="PENDING")
100 # is_online = models.BooleanField(default=False)
101 # is_active = models.BooleanField(default=False)
102 expires = models.DateField(default=days_offset)
103 response = models.CharField(max_length=512, blank=True, null=True)
104 comments = models.TextField(null=True, blank=True, verbose_name="Comments")
107 def __unicode__(self):
112 verbose_name = "Rule"
113 verbose_name_plural = "Rules"
115 def save(self, *args, **kwargs):
118 self.name = "%s_%s" %(self.name, hash)
119 super(Route, self).save(*args, **kwargs) # Call the "real" save() method.
122 def clean(self, *args, **kwargs):
123 from django.core.exceptions import ValidationError
126 address = IPNetwork(self.destination)
127 self.destination = address.exploded
129 raise ValidationError('Invalid network address format at Destination Field')
132 address = IPNetwork(self.source)
133 self.source = address.exploded
135 raise ValidationError('Invalid network address format at Source Field')
137 def commit_add(self, *args, **kwargs):
138 peer = self.applier.get_profile().peer.domain_name
139 send_message("[%s] Adding rule %s. Please wait..." %(self.applier.username, self.name), peer)
140 response = add.delay(self)
141 logger.info("Got add job id: %s" %response)
143 def commit_edit(self, *args, **kwargs):
144 peer = self.applier.get_profile().peer.domain_name
145 send_message("[%s] Editing rule %s. Please wait..." %(self.applier.username, self.name), peer)
146 response = edit.delay(self)
147 logger.info("Got edit job id: %s" %response)
149 def commit_delete(self, *args, **kwargs):
152 if "reason" in kwargs:
153 reason = kwargs['reason']
154 reason_text = "Reason: %s. " %reason
155 peer = self.applier.get_profile().peer.domain_name
156 send_message("[%s] Suspending rule %s. %sPlease wait..." %(self.applier.username, self.name, reason_text), peer)
157 response = delete.delay(self, reason=reason)
158 logger.info("Got delete job id: %s" %response)
160 def has_expired(self):
161 today = datetime.date.today()
162 if today > self.expires:
166 def check_sync(self):
167 if not self.is_synced():
168 self.status = "OUTOFSYNC"
173 get_device = PR.Retriever()
174 device = get_device.fetch_device()
176 routes = device.routing_options[0].routes
177 except Exception as e:
178 self.status = "EXPIRED"
180 logger.error("No routing options on device. Exception: %s" %e)
183 if route.name == self.name:
185 logger.info('Found a matching rule name')
186 devicematch = route.match
188 assert(self.destination)
189 assert(devicematch['destination'][0])
190 if self.destination == devicematch['destination'][0]:
191 found = found and True
192 logger.info('Found a matching destination')
195 logger.info('Destination fields do not match')
200 assert(devicematch['source'][0])
201 if self.source == devicematch['source'][0]:
202 found = found and True
203 logger.info('Found a matching source')
206 logger.info('Source fields do not match')
210 assert(self.fragmenttype)
211 assert(devicematch['fragment'][0])
212 if self.fragmenttype == devicematch['fragment'][0]:
213 found = found and True
214 logger.info('Found a matching fragment type')
217 logger.info('Fragment type fields do not match')
221 assert(self.icmpcode)
222 assert(devicematch['icmp-code'][0])
223 if self.icmpcode == devicematch['icmp-code'][0]:
224 found = found and True
225 logger.info('Found a matching icmp code')
228 logger.info('Icmp code fields do not match')
232 assert(self.icmptype)
233 assert(devicematch['icmp-type'][0])
234 if self.icmptype == devicematch['icmp-type'][0]:
235 found = found and True
236 logger.info('Found a matching icmp type')
239 logger.info('Icmp type fields do not match')
243 assert(self.protocol)
244 assert(devicematch['protocol'][0])
245 if self.protocol == devicematch['protocol'][0]:
246 found = found and True
247 logger.info('Found a matching protocol')
250 logger.info('Protocol fields do not match')
253 if found and self.status != "ACTIVE":
254 logger.error('Rule is applied on device but appears as offline')
255 self.status = "ACTIVE"
258 if self.status == "ADMININACTIVE" or self.status == "INACTIVE" or self.status == "EXPIRED":
264 then_statements = self.then.all()
265 for statement in then_statements:
266 if statement.action_value:
267 ret = "%s %s:<strong>%s</strong><br/>" %(ret, statement.action, statement.action_value)
269 ret = "%s %s<br>" %(ret, statement.action)
270 return ret.rstrip(',')
272 get_then.short_description = 'Then statement'
273 get_then.allow_tags = True
278 ret = '%s Dst Addr:<strong>%s</strong> <br/>' %(ret, self.destination)
279 if self.fragmenttype:
280 ret = "%s Fragment Type:<strong>%s</strong><br/>" %(ret, self.fragmenttype)
282 ret = "%s ICMP code:<strong>%s</strong><br/>" %(ret, self.icmpcode)
284 ret = "%s ICMP Type:<strong>%s</strong><br/>" %(ret, self.icmptype)
285 if self.packetlength:
286 ret = "%s Packet Length:<strong>%s</strong><br/>" %(ret, self.packetlength)
288 ret = "%s Protocol:<strong>%s</strong><br/>" %(ret, self.protocol)
290 ret = "%s Src Addr:<strong>%s</strong> <br/>" %(ret, self.source)
292 ret = "%s TCP flag:<strong>%s</strong><br/>" %(ret, self.tcpflag)
294 for port in self.port.all():
295 ret = ret + "Port:<strong>%s</strong> <br/>" %(port)
296 if self.destinationport:
297 for port in self.destinationport.all():
298 ret = ret + "Dst Port:<strong>%s</strong> <br/>" %(port)
300 for port in self.sourceport.all():
301 ret = ret +"Src Port:<strong>%s</strong> <br/>" %(port)
303 for dscp in self.dscp.all():
304 ret = ret + "%s Port:<strong>%s</strong> <br/>" %(ret, dscp)
305 return ret.rstrip('<br/>')
307 get_match.short_description = 'Match statement'
308 get_match.allow_tags = True
311 def applier_peer(self):
313 applier_peer = self.applier.get_profile().peer
319 def days_to_expire(self):
320 if self.status not in ['EXPIRED', 'ADMININACTIVE', 'ERROR', 'INACTIVE']:
321 expiration_days = (self.expires - datetime.date.today()).days
322 if expiration_days < settings.EXPIRATION_NOTIFY_DAYS:
323 return "%s" %expiration_days
329 def send_message(msg, user):
330 # username = user.username
332 b = beanstalkc.Connection()
333 b.use(settings.POLLS_TUBE)
334 tube_message = json.dumps({'message': str(msg), 'username':peer})