Exemplo n.º 1
0
    def LocalRequest(self):
        """Local request will trigger the session to send a ICRQ to remote,
        currently, this function is just for simulator usage.

        :return: None

        """
        self.logger.debug(
            "Got a local request to setup the session, will send a ICRQ to remote"
        )

        if isinstance(self.fsm, L2tpv3Fsm.L2tpv3SessionRecipientFsm):
            self.logger.warn(
                "Recipient does not support the local request, do nothing.")
            return

        if self.fsm.current == L2tpv3Fsm.L2tpv3SessionSenderFsm.StateIdle:
            msgAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.ICRQ)
            localSessionId = L2tpv3RFC3931AVPs.LocalSessionID(
                self.localSessionId)
            remoteSessionId = L2tpv3RFC3931AVPs.RemoteSessionID(
                self.remoteSessionId)
            remote_end_id = L2tpv3RFC3931AVPs.RemoteEndID(
                (((0, 3, 0), 0), ((0, 3, 1), 1)))
            DepiL2SpecificSublayerSubtype = L2tpv3CableLabsAvps.DepiL2SpecificSublayerSubtype(
                3)

            icrq = L2tpv3ControlPacket.L2tpv3ControlPacket(
                self.connection.remoteConnID,
                avps=(msgAvp, localSessionId, remoteSessionId, remote_end_id,
                      DepiL2SpecificSublayerSubtype))
            self.connection.transport.SendPacket(icrq)

        self.fsm.localRequest()
Exemplo n.º 2
0
    def __init__(self, session, resultCode, errCode, errMsg):
        connection = session.connection
        super(
            L2tpv3CDN, self).__init__(L2tpv3RFC3931AVPs.ControlMessageAVP.CDN, connection.remoteConnID,
                                      resultCode, errCode, errMsg)
        localSessionID = session.localSessionId
        remoteSessionID = session.remoteSessionId

        localAvp = L2tpv3RFC3931AVPs.LocalSessionID(localSessionID)
        remoteAvp = L2tpv3RFC3931AVPs.RemoteSessionID(remoteSessionID)
        self.avps.append(localAvp)
        self.avps.append(remoteAvp)
Exemplo n.º 3
0
    def localRequest(self, addr):
        # send a SCCRQ to remote

        msgAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
            L2tpv3RFC3931AVPs.ControlMessageAVP.SCCRQ)
        assignedAvp = L2tpv3RFC3931AVPs.AssignedControlConnectionID(
            self.localConnID)
        sccrq = L2tpv3ControlPacket.L2tpv3ControlPacket(0,
                                                        avps=(msgAvp,
                                                              assignedAvp))
        self.transport.SendPacket(sccrq, (addr, 0))

        self.fsm.localRequest()
Exemplo n.º 4
0
    def setUpClass(cls):
        # open logger
        setup_logging("L2TP")
        setup_test_redis()

        cls.conn_address = '127.0.0.1'
        global_dispatcher = Dispatcher()
        dispatcher = L2tpv3Dispatcher(
            global_dispatcher, cls.conn_address, False, None)
        L2tpv3GlobalSettings.Dispatcher = dispatcher
        # setup the halclient

        notification_list = list()
        notification_list.append(
            HalConfigMsg.MsgTypeL2tpv3SessionStatusNotification)

        cls.hal_client = L2tpHalClient("L2TP_HAL_CLIENT",
                                       "the HAL client of L2TP feature",
                                       "1.0", tuple(L2tpHalClient.notification_list.keys()), global_dispatcher)

        cls.hal_client.handler = dispatcher.receive_hal_message
        cls.conn = L2tpConnection(
            6661, 6662, cls.conn_address, cls.conn_address)
        cls.session = L2tpv3Session(6661, 6662, 'receiver', cls.conn)
        cls.conn.addSession(cls.session)
        localSessionId = L2tpv3RFC3931AVPs.LocalSessionID(6661)
        remoteSessionId = L2tpv3RFC3931AVPs.RemoteSessionID(6662)
        remoteEnd = L2tpv3RFC3931AVPs.RemoteEndID(
            (((0, 3, 0), 0), ((0, 3, 1), 1), ((0, 3, 2), 2)))
        remoteEnd_1 = L2tpv3RFC3931AVPs.RemoteEndID(
            (((0, 3, 3), 3), ((0, 3, 4), 4), ((0, 3, 5), 5)))
        pw_type = L2tpv3RFC3931AVPs.L2SpecificSublayer(3)
        DepiL2SpecificSublayerSubtype = L2tpv3CableLabsAvps.DepiL2SpecificSublayerSubtype(3)
        LocalMTUCableLabs = L2tpv3CableLabsAvps.LocalMTUCableLabs(1500)
        DepiRemoteMulticastJoin = L2tpv3CableLabsAvps.DepiRemoteMulticastJoin(("5.5.5.1", "229.1.1.255"))
        DepiResourceAllocReq = L2tpv3CableLabsAvps.DepiResourceAllocReq(((0, 1), (1, 2)))
        UpstreamFlow = L2tpv3CableLabsAvps.UpstreamFlow(((0, 1), (1, 2)))

        cls.session.avps_icrq.append(localSessionId)
        cls.session.avps_icrq.append(remoteSessionId)
        cls.session.avps_icrq.append(remoteEnd)
        cls.session.avps_icrq.append(remoteEnd_1)
        cls.session.avps_icrq.append(DepiL2SpecificSublayerSubtype)
        cls.session.avps_icrq.append(LocalMTUCableLabs)
        cls.session.avps_icrq.append(pw_type)
        cls.session.avps_icrq.append(DepiRemoteMulticastJoin)
        cls.session.avps_icrq.append(DepiResourceAllocReq)
        cls.session.avps_icrq.append(UpstreamFlow)
Exemplo n.º 5
0
    def recvSCCRQ(self, pkt):
        """Receive SCCRQ."""
        if L2tpv3GlobalSettings.L2tpv3GlobalSettings.MustAvpsCheck is True:
            ret = self.checkMustAvps(
                L2tpv3ControlPacket.L2tpv3ControlPacket.SCCRQMandatoryAVPs,
                pkt.avps)
            if ret is not True:
                self.fsm.recvBadSCCRQ()
                return L2tpv3ControlPacket.L2tpv3StopCCN(
                    self, 2, 4, "Avp cannot handled correctly")
        sccrpAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
            L2tpv3RFC3931AVPs.ControlMessageAVP.SCCRP)
        recvWinSize = ReceiveWinSize(
            L2tpv3GlobalSettings.L2tpv3GlobalSettings.ReceiveWindowSize)

        sccrp = L2tpv3ControlPacket.L2tpv3ControlPacket(
            self.remoteConnID, 0, 0, (sccrpAvp, recvWinSize))

        if len(pkt.avps) > 1:
            for i in xrange(1, len(pkt.avps)):
                avp = pkt.avps[i]
                # We got a bad SCCRQ, we should send a CDN
                if not avp.handleAvp(pkt, sccrp):
                    self.fsm.recvBadSCCRQ()
                    return L2tpv3ControlPacket.L2tpv3StopCCN(
                        self, 2, 4, "Avp cannot handled correctly")
        self.fsm.recvGoodSCCRQ()
        return sccrp
Exemplo n.º 6
0
 def __init__(self, connection, resultCode, errCode, errMsg):
     super(
         L2tpv3StopCCN, self).__init__(L2tpv3RFC3931AVPs.ControlMessageAVP.StopCCN, connection.remoteConnID,
                                       resultCode, errCode, errMsg)
     assignedConnectionAvp = L2tpv3RFC3931AVPs.AssignedControlConnectionID(
         connection.localConnID)
     self.avps.append(assignedConnectionAvp)
Exemplo n.º 7
0
    def recvFSQ(self, pkt):
        """Receive a FSQ packet.

        :param pkt: Decoded control packet.
        :return: FSR.

        """
        self.logger.debug("Receive a FSQ message from remote")
        if L2tpv3GlobalSettings.L2tpv3GlobalSettings.MustAvpsCheck is True:
            ret = self.checkMustAvps(
                L2tpv3ControlPacket.L2tpv3ControlPacket.FSQMandatoryAVPs,
                pkt.avps)
            if ret is not True:
                return

        fsrAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
            L2tpv3RFC3931AVPs.ControlMessageAVP.FSR)
        fsr = L2tpv3ControlPacket.L2tpv3ControlPacket(self.remoteConnID, 0, 0,
                                                      (fsrAvp, ))

        if len(pkt.avps) > 1:
            for i in xrange(1, len(pkt.avps)):
                avp = pkt.avps[i]
                avp.handleAvp(pkt, fsr)

        return fsr
Exemplo n.º 8
0
    def test_append_VspAvp(self):
        print "test_append_VspAvp\n"

        vsp_avp_attrType1 = 10
        vsp_avp_attrType2 = 20
        vsp_avp1 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType1,
                                         attrValue="11111111111")
        vsp_avp2 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType2,
                                         notifyVendor=1,
                                         attrValue="222222222222")

        # vsp_avp3 has OutCtrlPktList set to L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP, and it will be
        # appended to the avps list, and vsp_avp1 will not.
        ctrlIdsList = []
        ctrlIdsList.append(L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP)
        vsp_avp_attrType3 = 30
        vsp_avp3_attrValue = "3333333333"
        vsp_avp3 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType3,
                                         OutCtrlIds=ctrlIdsList,
                                         attrValue=vsp_avp3_attrValue)

        vsp_avps = l2tpv3VspAvps()
        self.assertTrue(vsp_avps is not None)
        vsp_avps.add_VspAvp(vsp_avp1)
        vsp_avps.add_VspAvp(vsp_avp2)
        vsp_avps.add_VspAvp(vsp_avp3)

        avps = list()
        avps.append(
            L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP))
        avps.append(
            L2tpv3RFC3931AVPs.SbfdVccv(L2tpv3RFC3931AVPs.SbfdVccv.VccvValue))

        vsp_avps.append_VspAvp(avps, L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP)
        # vsp_avp3 should be in avps list now
        found_vsp_avp_in_icrp = 0
        for avp in avps:
            if (isinstance(avp, l2tpv3SampleVendorAvp)
                    and avp.attrValue == vsp_avp3_attrValue):
                found_vsp_avp_in_icrp = 1
        self.assertEqual(found_vsp_avp_in_icrp, 1)
Exemplo n.º 9
0
 def sendSLI(self):
     if self.fsm.current == L2tpv3Fsm.L2tpv3SessionRecipientFsm.StateEstablished and\
             self.connection and self.connection.transport:
         msgAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
             L2tpv3RFC3931AVPs.ControlMessageAVP.SLI)
         localSessionId = L2tpv3RFC3931AVPs.LocalSessionID(
             self.localSessionId)
         remoteSessionId = L2tpv3RFC3931AVPs.RemoteSessionID(
             self.remoteSessionId)
         circuitstatus = L2tpv3RFC3931AVPs.CircuitStatus(
             active=self.local_circuit_status, new=False)
         sli = L2tpv3ControlPacket.L2tpv3ControlPacket(
             self.connection.remoteConnID,
             avps=(msgAvp, localSessionId, remoteSessionId, circuitstatus))
         self.connection.transport.SendPacket(sli)
         self.logger.info("send SLI status change to remote: " + "local:" +
                          str(self.localSessionId) + " remote:" +
                          str(self.remoteSessionId) + " status:" +
                          str(self.local_circuit_status))
Exemplo n.º 10
0
    def test_handleAvp(self):
        vsp_avp_attrType1 = 10
        vsp_avp_attrType2 = 20
        vsp_avp1 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType1,
                                         attrValue="11111111111")
        vsp_avp2 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType2,
                                         notifyVendor=1,
                                         attrValue="222222222222")
        ctrlIdsList = []
        ctrlIdsList.append(L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP)
        vsp_avp_attrType3 = 30
        vsp_avp3_attrValue = "3333333333"
        vsp_avp3 = l2tpv3SampleVendorAvp(DEFAULT_VENDOR_ID,
                                         vsp_avp_attrType3,
                                         OutCtrlIds=ctrlIdsList,
                                         attrValue=vsp_avp3_attrValue)

        # Receive a good ICRQ, send a ICRP
        session_receiver = L2tpv3Session(0, 1, 'receiver')
        icrq = L2tpv3ControlPacket.decode(self.icrq_buf)
        # append couple VSP AVP
        icrq.avps.append(vsp_avp1)
        icrq.avps.append(vsp_avp2)
        icrq.avps.append(vsp_avp3)
        # print icrq

        # icrq.SetPktConnection(self.conn)
        # icrq.SetPktSession(session_receiver)
        #icrp = session_receiver.ReceiveICRQ(icrq)
        # Observe that only vsp_avp3 is appended to icrp
        # self.assertEqual(
        #    session_receiver.fsm.current, L2tpv3SessionRecipientFsm.StateWaitConn)
        #self.assertEqual(icrp.avps[0].messageType, ControlMessageAVP.ICRP)

        avps = list()
        avps.append(
            L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP))
        icrp = L2tpv3ControlPacket(6600, 0, 0, avps)
        for avp in icrq.avps:
            avp.handleAvp(icrq, icrp)

        # vsp_avp3 has non-None OutCtrlIds so it should be moved from icrq to icrp now
        found_vsp_avp_in_icrp = 0
        for avp in icrp.avps:
            if (isinstance(avp, l2tpv3SampleVendorAvp)
                    and avp.attrValue == vsp_avp3_attrValue):
                found_vsp_avp_in_icrp = 1

        self.assertEqual(found_vsp_avp_in_icrp, 1)
Exemplo n.º 11
0
    def queryInactSessions(self):
        inActiveSessions = list()
        keys = self.sessions.keys()
        for sessionId in keys:
            session = self.sessions[sessionId]
            if session.local_circuit_status != L2tpv3Session.L2tpv3Session.CIRCUIT_STATUS_UP:
                self.logger.debug(
                    "query for inactive session [%d, %d]" %
                    (session.localSessionId, session.remoteSessionId))
                inActiveSessions.append(session)

        if inActiveSessions:
            fsqAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.FSQ)
            fssAvps = tuple()
            for ses in inActiveSessions:
                fss = L2tpv3RFC3931AVPs.FailoverSessionState(
                    ses.localSessionId, ses.remoteSessionId)
                fssAvps = fssAvps + (fss, )
            fsq = L2tpv3ControlPacket.L2tpv3ControlPacket(
                self.remoteConnID, 0, 0, (fsqAvp, ) + fssAvps)
            self.transport.SendPacket(fsq)
Exemplo n.º 12
0
    def queryStaleSessions(self):
        staleSessions = list()
        keys = self.sessions.keys()
        for sessionId in keys:
            session = self.sessions[sessionId]
            if session.stale:
                self.logger.debug(
                    "query for stale session [%d, %d]" %
                    (session.localSessionId, session.remoteSessionId))
                staleSessions.append(session)

        if staleSessions:
            fsqAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.FSQ)
            fssAvps = tuple()
            for ses in staleSessions:
                fss = L2tpv3RFC3931AVPs.FailoverSessionState(
                    ses.localSessionId, ses.remoteSessionId)
                fssAvps = fssAvps + (fss, )
            fsq = L2tpv3ControlPacket.L2tpv3ControlPacket(
                self.remoteConnID, 0, 0, (fsqAvp, ) + fssAvps)
            self.transport.SendPacket(fsq)
Exemplo n.º 13
0
    def recvSCCRP(self, pkt):
        """On openRPD, we will behave as an recipient, so we will not receive
        SCCRP in normal case.

        :param pkt:
        :return:

        """
        self.logger.debug("Receive a SCCRP packet")

        if L2tpv3GlobalSettings.L2tpv3GlobalSettings.MustAvpsCheck is True:
            ret = self.checkMustAvps(
                L2tpv3ControlPacket.L2tpv3ControlPacket.SCCRPMandatoryAVPs,
                pkt.avps)
            if ret is not True:
                self.fsm.recvBadSCCRP()
                return L2tpv3ControlPacket.L2tpv3StopCCN(
                    self, 2, 4, "Avp cannot handled correctly")
        # Get the localAssignedID from the SCCRP
        remoteConnID = 0
        for avp in pkt.avps:
            if isinstance(avp, L2tpv3RFC3931AVPs.AssignedControlConnectionID):
                remoteConnID = avp.connectionID

        self.remoteConnID = remoteConnID
        scccnAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
            L2tpv3RFC3931AVPs.ControlMessageAVP.SCCCN)
        scccn = L2tpv3ControlPacket.L2tpv3ControlPacket(
            self.remoteConnID, 0, 0, (scccnAvp, ))

        if len(pkt.avps) > 1:
            for i in xrange(1, len(pkt.avps)):
                avp = pkt.avps[i]
                # We got a bad ICRQ, we should send a CDN
                if not avp.handleAvp(pkt, scccn):
                    self.fsm.recvBadSCCRP()
                    return L2tpv3ControlPacket.L2tpv3StopCCN(
                        self, 2, 4, "Avp cannot handled correctly")

        self.fsm.recvGoodSCCRP()
        return scccn
Exemplo n.º 14
0
 def __init__(self, connID):
     helloavp = L2tpv3RFC3931AVPs.ControlMessageAVP(
         L2tpv3RFC3931AVPs.ControlMessageAVP.HELLO)
     super(L2tpv3Hello, self).__init__(
         remoteConnID=connID, avps=(helloavp,))
Exemplo n.º 15
0
    def setUpClass(cls):
        setupDB()
        time.sleep(2)

        HalGlobal.gClientMgr = None
        HalGlobal.gPoller = None
        t = threading.Thread(target=demoHalmain)
        t.daemon = True
        t.start()
        time.sleep(2)
        threads_list.append(t)
        if not HalGlobal.gClientMgr or not HalGlobal.gPoller:
            raise Exception("Cannot start the demo halMain")
        t = threading.Thread(target=demoL2tp)
        t.daemon = True
        t.start()
        time.sleep(2)
        threads_list.append(t)
        t = threading.Thread(target=demoDrvmain)
        t.daemon = True
        t.start()
        threads_list.append(t)
        time.sleep(2)

        setup_logging('HAL', filename="hal.log", logging_level=logging.DEBUG)
        cls.logger = logging.getLogger("L2tpHalDrvVspAvpTest")
        cls.logger.info("hello L2tpHalDrvVspAvpTest Log")

        cls.stop = False
        cls.conn_address = '127.0.0.1'
        # Setup connection/session: set it here since global variables are already only after threads are up.
        cls.dispatcher = L2tpv3GlobalSettings.Dispatcher
        cls.hal_client = L2tpv3GlobalSettings.l2tp_hal_client
        # cls.conn = L2tpConnection.L2tpConnection(
        #    6661, 6662, cls.conn_address, cls.conn_address)
        #cls.session = L2tpv3Session.L2tpv3Session(6661, 6662, 'receiver',cls.conn)
        # cls.conn.addSession(cls.session)
        localSessionId = L2tpv3RFC3931AVPs.LocalSessionID(6661)
        remoteSessionId = L2tpv3RFC3931AVPs.RemoteSessionID(6662)
        remoteEnd = L2tpv3RFC3931AVPs.RemoteEndID(
            (((0, 3, 0), 0), ((0, 3, 1), 1), ((0, 3, 2), 2)))
        remoteEnd_1 = L2tpv3RFC3931AVPs.RemoteEndID(
            (((0, 3, 3), 3), ((0, 3, 4), 4), ((0, 3, 5), 5)))
        pw_type = L2tpv3RFC3931AVPs.L2SpecificSublayer(3)
        DepiL2SpecificSublayerSubtype = L2tpv3CableLabsAvps.DepiL2SpecificSublayerSubtype(
            3)
        LocalMTUCableLabs = L2tpv3CableLabsAvps.LocalMTUCableLabs(1500)

        cls.avps_icrq = []
        cls.avps_icrq.append(localSessionId)
        cls.avps_icrq.append(remoteSessionId)
        cls.avps_icrq.append(remoteEnd)
        cls.avps_icrq.append(remoteEnd_1)
        cls.avps_icrq.append(DepiL2SpecificSublayerSubtype)
        cls.avps_icrq.append(LocalMTUCableLabs)
        cls.avps_icrq.append(pw_type)

        cls.icrq_buf = struct.pack(
            '!206B',
            0xc8,
            0x03,
            0x0,
            206,
            0x0,
            0x0,
            0x0,
            0x0,
            0x0,
            0x3,
            0x0,
            0x4,
            0xc,
            8,
            0x0,
            0x0,
            0x0,
            0x0,
            0x0,
            10,
            0xc,
            10,
            0x0,
            0x0,
            0,
            15,
            0,
            0,
            0,
            0,
            0xc,
            10,
            0x0,
            0x0,
            0,
            63,
            0x40,
            0x01,
            0x00,
            0x01,
            0xc,
            10,
            0x0,
            0x0,
            0,
            64,
            0x0,
            0x0,
            0x0,
            0x0,
            0xc,
            40,
            0x0,
            0x0,
            0x0,
            66,
            0x0,
            0x0,
            0x00,
            0x03,
            0x00,
            0x00,
            0x00,
            0x03,
            0x01,
            0x01,
            0x00,
            0x03,
            0x02,
            0x02,
            0x00,
            0x03,
            0x03,
            0x03,
            0x00,
            0x03,
            0x04,
            0x04,
            0x00,
            0x03,
            0x05,
            0x05,
            0x00,
            0x03,
            0x06,
            0x06,
            0x00,
            0x03,
            0x07,
            0x07,
            0xc,
            8,
            0,
            0,
            0,
            68,
            0,
            12,
            0xc,
            8,
            0,
            0,
            0,
            69,
            0,
            3,
            0xc,
            8,
            0,
            0,
            0,
            71,
            0,
            2,
            0xc,
            8,
            0x11,
            0x8b,
            0x0,
            0x2,
            0x1,
            0x0,
            0xc,
            8,
            0x11,
            0x8b,
            0x0,
            0x4,
            0x7,
            0xD0,
            0xc,
            20,
            0x11,
            0x8b,
            0x0,
            15,
            0x0,
            0x1,
            0x0,
            0x2,
            0x0,
            0x3,
            0x0,
            0x6,
            0x0,
            0x8,
            0x0,
            11,
            0x0,
            13,
            0xc,
            8,
            0x11,
            0x8b,
            0x0,
            16,
            0x0,
            0x3,
            0xc,
            8,
            0x11,
            0x8b,
            0x0,
            17,
            0x0,
            0x3,
            0xc,
            40,
            0x11,
            0x8b,
            0x0,
            11,
            0,
            0,
            0x5,
            0x6,
            0x7,
            0x8,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            229,
            1,
            1,
            255,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
        )
Exemplo n.º 16
0
        sessionID = avp.sessionID
        break

iccn.ns = 3
iccn.nr = 2
iccn.connectionID = conntionID

for avp in iccn.avps:
    if isinstance(avp, L2tpv3RFC3931AVPs.RemoteSessionID):
        avp.sessionID = sessionID
# Change the AVPs
print iccn
buf = iccn.encode(True)
s.sendto(buf, ('127.0.0.1', 1))

cdnMsg = L2tpv3RFC3931AVPs.ControlMessageAVP(
    L2tpv3RFC3931AVPs.ControlMessageAVP.CDN)
retcode = L2tpv3RFC3931AVPs.ResultCode(
    L2tpv3RFC3931AVPs.ControlMessageAVP.StopCCN, 0, 0, "test")
# Get the lcoal session ID
localAvp = None
for avp in iccn.avps:
    if isinstance(avp, L2tpv3RFC3931AVPs.LocalSessionID):
        lcoalSessionID = avp.sessionID
        localAvp = L2tpv3RFC3931AVPs.LocalSessionID(lcoalSessionID)
remoteAvp = L2tpv3RFC3931AVPs.RemoteSessionID(sessionID)
cdn = L2tpv3ControlPacket.L2tpv3ControlPacket(conntionID,
                                              avps=(cdnMsg, retcode, localAvp,
                                                    remoteAvp))

cdn.ns = 4
cdn.nr = 2
Exemplo n.º 17
0
    def ReceiveICRQ(self, pkt):
        """Receive a ICRQ from remote, if it is a good ICRQ, will send a ICRP.

        :param pkt: The ICRQ control packet, has been decoded.
        :return: ICRP packet or None

        """

        if L2tpv3GlobalSettings.L2tpv3GlobalSettings.MustAvpsCheck is True:
            ret = self.connection.checkMustAvps(
                L2tpv3ControlPacket.L2tpv3ControlPacket.ICRQMandatoryAVPs,
                pkt.avps)
            if ret is not True:
                self.fsm.recvBadICRQ()
                return L2tpv3ControlPacket.L2tpv3CDN(
                    self, 2, 4, "Avp cannot be handled correctly")

        self.logger.debug("L2tp session[%d, %d] receives a ICRQ message.",
                          self.localSessionId, self.remoteSessionId)

        avps = list()
        avps.append(
            L2tpv3RFC3931AVPs.ControlMessageAVP(
                L2tpv3RFC3931AVPs.ControlMessageAVP.ICRP))
        avps.append(
            L2tpv3RFC3931AVPs.DataSequencing(
                L2tpv3RFC3931AVPs.DataSequencing.AllSeq))
        # TODO  add sbfd support for ipv6
        if Convert.is_valid_ipv4_address(self.connection.localAddr):
            avps.append(
                L2tpv3RFC3931AVPs.SbfdDiscriminator(
                    int(
                        socket.inet_aton(
                            self.connection.localAddr).encode('hex'), 16)))
            avps.append(
                L2tpv3RFC3931AVPs.SbfdVccv(
                    L2tpv3RFC3931AVPs.SbfdVccv.VccvValue))

        # Need add some Cable labs avp
        self.logger.debug(
            "Session [%d, %d]sends a ICRP packet to remote, connection:%d",
            self.localSessionId, self.remoteSessionId,
            pkt.Connection.remoteConnID)

        icrp = L2tpv3ControlPacket.L2tpv3ControlPacket(
            pkt.Connection.remoteConnID, 0, 0, avps)
        del self.avps_icrq[:]
        del self.mcast[:]
        if len(pkt.avps) > 1:
            for i in xrange(1, len(pkt.avps)):
                avp = pkt.avps[i]
                if isinstance(
                        avp,
                        L2tpv3CableLabsAvps.DepiL2SpecificSublayerSubtype):
                    self.session_l2Sublayer = avp.pw_type
                self.avps_icrq.append(avp)
                # We got a bad ICRQ, we should send a CDN
                if not avp.handleAvp(pkt, icrp):
                    self.fsm.recvBadICRQ()
                    return L2tpv3ControlPacket.L2tpv3CDN(
                        self, 2, 4, "Avp cannot be handled correctly")
        self.logger.debug("We got a good ICRQ, send to fsm")
        self.fsm.recvGoodICRQ()
        return icrp
Exemplo n.º 18
0
    def ReceiveICRP(self, pkt):
        """Receive a ICRP from remote, if it is a good ICRP, will send a ICCN.
        this function will be used for simulator purpose.

        :param pkt: The ICRP control packet, has been decoded.
        :return:

        """
        if L2tpv3GlobalSettings.L2tpv3GlobalSettings.MustAvpsCheck is True:
            ret = self.connection.checkMustAvps(
                L2tpv3ControlPacket.L2tpv3ControlPacket.ICRPMandatoryAVPs,
                pkt.avps)
            if ret is not True:
                if isinstance(self.fsm, L2tpv3Fsm.L2tpv3SessionSenderFsm):
                    self.fsm.recvBadICRP()
                return L2tpv3ControlPacket.L2tpv3CDN(
                    self, 2, 4, "Avp cannot be handled correctly")

        self.logger.debug("Session [%d, %d] gets a l2tp ICRP message.",
                          self.localSessionId, self.remoteSessionId)

        if isinstance(self.fsm, L2tpv3Fsm.L2tpv3SessionRecipientFsm):
            self.logger.debug(
                "Recipient session [%d, %d] gets a l2tp ICRP message.",
                self.localSessionId, self.remoteSessionId)
            self.fsm.recvICRP()
            # this event will trigger the recipient state
            # machine transferring to idle state.
            return

        # Find the local session ID
        for avp in pkt.avps:
            if isinstance(avp, L2tpv3RFC3931AVPs.LocalSessionID):
                self.remoteSessionId = avp.sessionID
                self.connection.addSession(self)

        # If the incoming l2TP ICRP does not contain a local session ID
        if not self.remoteSessionId:
            self.logger.warn(
                "Session[%d, %d] is terminated due to not find the local session ID in ICRP message.",
                self.localSessionId, self.remoteSessionId)
            self.fsm.recvBadICRP()
            return L2tpv3ControlPacket.L2tpv3CDN(self, 2, 5, "")

        # send a ICCN
        msgAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(
            L2tpv3RFC3931AVPs.ControlMessageAVP.ICCN)
        iccn = L2tpv3ControlPacket.L2tpv3ControlPacket(
            self.connection.remoteConnID, avps=(msgAvp, ))
        if len(pkt.avps) > 1:
            for i in xrange(1, len(pkt.avps)):
                avp = pkt.avps[i]
                # We got a bad ICRP, we should send a CDN
                if not avp.handleAvp(pkt, iccn):
                    self.fsm.recvBadICRP()
                    return L2tpv3ControlPacket.L2tpv3CDN(
                        self, 2, 4, "Avp cannot be handled correctly")

        self.logger.debug(
            "Sender session [%d, %d] gets a good l2tp ICRP message.",
            self.localSessionId, self.remoteSessionId)
        self.fsm.recvGoodICRP()
        return iccn
Exemplo n.º 19
0
 def __init__(self, connID):
     ackavp = L2tpv3RFC3931AVPs.ControlMessageAVP(
         L2tpv3RFC3931AVPs.ControlMessageAVP.ACK)
     super(L2tpv3ACK, self).__init__(
         remoteConnID=connID, avps=(ackavp,))
     self.isZlb = True
Exemplo n.º 20
0
 def __init__(self, msgType, connID, resultCode, errCode, errMsg):
     stopAvp = L2tpv3RFC3931AVPs.ControlMessageAVP(msgType)
     retcode = L2tpv3RFC3931AVPs.ResultCode(
         L2tpv3RFC3931AVPs.ControlMessageAVP.StopCCN, resultCode, errCode, errMsg)
     super(l2tpV3TerminatePkt, self).__init__(
         remoteConnID=connID, avps=(stopAvp, retcode))