1## This file is part of Scapy
2## See http://www.secdev.org/projects/scapy for more informations
3## Copyright (C) Philippe Biondi <phil@secdev.org>
4## This program is published under a GPLv2 license
5
6"""
7RIP (Routing Information Protocol).
8"""
9
10from scapy.packet import *
11from scapy.fields import *
12from scapy.layers.inet import UDP
13
14class RIP(Packet):
15    name = "RIP header"
16    fields_desc = [
17        ByteEnumField("cmd", 1, {1:"req", 2:"resp", 3:"traceOn", 4:"traceOff",
18                                 5:"sun", 6:"trigReq", 7:"trigResp", 8:"trigAck",
19                                 9:"updateReq", 10:"updateResp", 11:"updateAck"}),
20        ByteField("version", 1),
21        ShortField("null", 0),
22        ]
23
24    def guess_payload_class(self, payload):
25        if payload[:2] == "\xff\xff":
26            return RIPAuth
27        else:
28            return Packet.guess_payload_class(self, payload)
29
30class RIPEntry(RIP):
31    name = "RIP entry"
32    fields_desc = [
33        ShortEnumField("AF", 2, {2:"IP"}),
34        ShortField("RouteTag", 0),
35        IPField("addr", "0.0.0.0"),
36        IPField("mask", "0.0.0.0"),
37        IPField("nextHop", "0.0.0.0"),
38        IntEnumField("metric", 1, {16:"Unreach"}),
39        ]
40
41class RIPAuth(Packet):
42    name = "RIP authentication"
43    fields_desc = [
44        ShortEnumField("AF", 0xffff, {0xffff:"Auth"}),
45        ShortEnumField("authtype", 2, {1:"md5authdata", 2:"simple", 3:"md5"}),
46        ConditionalField(StrFixedLenField("password", None, 16),
47                                        lambda pkt: pkt.authtype == 2),
48        ConditionalField(ShortField("digestoffset", 0),
49                                        lambda pkt: pkt.authtype == 3),
50        ConditionalField(ByteField("keyid", 0),
51                                        lambda pkt: pkt.authtype == 3),
52        ConditionalField(ByteField("authdatalen", 0),
53                                        lambda pkt: pkt.authtype == 3),
54        ConditionalField(IntField("seqnum", 0),
55                                        lambda pkt: pkt.authtype == 3),
56        ConditionalField(StrFixedLenField("zeropad", None, 8),
57                                        lambda pkt: pkt.authtype == 3),
58        ConditionalField(StrLenField("authdata", None,
59                                length_from=lambda pkt: pkt.md5datalen),
60                                        lambda pkt: pkt.authtype == 1)
61        ]
62
63    def pre_dissect(self, s):
64        if s[2:4] == "\x00\x01":
65            self.md5datalen = len(s) - 4
66
67        return s
68
69
70bind_layers( UDP,           RIP,           sport=520)
71bind_layers( UDP,           RIP,           dport=520)
72bind_layers( RIP,           RIPEntry,      )
73bind_layers( RIPEntry,      RIPEntry,      )
74bind_layers( RIPAuth,       RIPEntry,      )
75