1f7d24e3fSHanoh Haim## This file is part of Scapy
2f7d24e3fSHanoh Haim## See http://www.secdev.org/projects/scapy for more informations
3f7d24e3fSHanoh Haim## Copyright (C) Philippe Biondi <phil@secdev.org>
4f7d24e3fSHanoh Haim## This program is published under a GPLv2 license
5f7d24e3fSHanoh Haim
6f7d24e3fSHanoh Haim"""
7f7d24e3fSHanoh HaimL2TP (Layer 2 Tunneling Protocol) for VPNs.
8f7d24e3fSHanoh Haim
9f7d24e3fSHanoh Haim[RFC 2661]
10f7d24e3fSHanoh Haim"""
11f7d24e3fSHanoh Haim
12f7d24e3fSHanoh Haimimport struct
13f7d24e3fSHanoh Haim
14f7d24e3fSHanoh Haimfrom scapy.packet import *
15f7d24e3fSHanoh Haimfrom scapy.fields import *
16f7d24e3fSHanoh Haimfrom scapy.layers.inet import UDP
17f7d24e3fSHanoh Haimfrom scapy.layers.ppp import PPP
18f7d24e3fSHanoh Haim
19f7d24e3fSHanoh Haimclass L2TP(Packet):
20f7d24e3fSHanoh Haim    fields_desc = [ ShortEnumField("pkt_type",2,{2:"data"}),
21f7d24e3fSHanoh Haim                    ShortField("len", None),
22f7d24e3fSHanoh Haim                    ShortField("tunnel_id", 0),
23f7d24e3fSHanoh Haim                    ShortField("session_id", 0),
24f7d24e3fSHanoh Haim                    ShortField("ns", 0),
25f7d24e3fSHanoh Haim                    ShortField("nr", 0),
26f7d24e3fSHanoh Haim                    ShortField("offset", 0) ]
27f7d24e3fSHanoh Haim
28f7d24e3fSHanoh Haim    def post_build(self, pkt, pay):
29f7d24e3fSHanoh Haim        if self.len is None:
30f7d24e3fSHanoh Haim            l = len(pkt)+len(pay)
31f7d24e3fSHanoh Haim            pkt = pkt[:2]+struct.pack("!H", l)+pkt[4:]
32f7d24e3fSHanoh Haim        return pkt+pay
33f7d24e3fSHanoh Haim
34f7d24e3fSHanoh Haim
35f7d24e3fSHanoh Haimbind_layers( UDP,           L2TP,          sport=1701, dport=1701)
36f7d24e3fSHanoh Haimbind_layers( L2TP,          PPP,           )
37